0% found this document useful (0 votes)
12 views

Selenium Methods

Uploaded by

Akshaya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Selenium Methods

Uploaded by

Akshaya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

get(String URL)

 Use: Opens the specified URL in the browser.

driver.get("https://example.com");

2. getTitle()

 Use: Returns the title of the current web page.

String title = driver.getTitle();

System.out.println(title);

3. getCurrentUrl()

 Use: Retrieves the URL of the current page.

String currentUrl = driver.getCurrentUrl();

System.out.println(currentUrl);

4. getPageSource()

 Use: Fetches the HTML source code of the current page.

String pageSource = driver.getPageSource();

System.out.println(pageSource);

5. close()

 Use: Closes the current browser window.

driver.close();

6. quit()

 Use: Closes all the browser windows opened by WebDriver.

driver.quit();

7. findElement(By locator)

 Use: Finds the first web element matching the specified locator.

WebElement element = driver.findElement(By.id("elementId"));


8. findElements(By locator)

 Use: Finds all elements matching the specified locator and returns a list.

List<WebElement> elements = driver.findElements(By.className("className"));

9. sendKeys(CharSequence... keysToSend)

 Use: Types text into an input field or textarea.

WebElement inputField = driver.findElement(By.name("q"));

inputField.sendKeys("Selenium WebDriver");

10. click()

 Use: Clicks on an element.

WebElement button = driver.findElement(By.id("submitBtn"));

button.click();

11. clear()

 Use: Clears the text of an input field or textarea.

WebElement inputField = driver.findElement(By.name("q"));

inputField.clear();

12. submit()

 Use: Submits a form.

WebElement form = driver.findElement(By.id("formId"));

form.submit();

13. isDisplayed()

 Use: Checks if an element is visible on the page.

boolean visible = driver.findElement(By.id("elementId")).isDisplayed();

14. isEnabled()

 Use: Checks if an element is enabled for interaction.

boolean enabled = driver.findElement(By.id("submitBtn")).isEnabled();


15. isSelected()

 Use: Checks if a checkbox, radio button, or option is selected.

boolean selected = driver.findElement(By.id("checkboxId")).isSelected();

16. getText()

 Use: Retrieves the inner text of a web element.

String text = driver.findElement(By.id("message")).getText();

17. getAttribute(String attributeName)

 Use: Retrieves the value of a specified attribute of a web element.

String value = driver.findElement(By.name("username")).getAttribute("value");

18. getCssValue(String propertyName)

 Use: Retrieves the CSS value of a specified property for a web element.

String color = driver.findElement(By.id("header")).getCssValue("color");

19. navigate().to(String URL)

 Use: Navigates to a specific URL.

driver.navigate().to("https://example.com");

20. navigate().back()

 Use: Navigates back in the browser's history.

driver.navigate().back();

21. navigate().forward()

 Use: Navigates forward in the browser's history.

driver.navigate().forward();

22. navigate().refresh()

 Use: Refreshes the current page.


driver.navigate().refresh();

23. manage().window().maximize()

 Use: Maximizes the browser window.

driver.manage().window().maximize();

24. manage().window().getSize()

 Use: Gets the size of the current browser window.

Dimension size = driver.manage().window().getSize();

System.out.println(size);

25. manage().window().setSize(Dimension dimension)

 Use: Sets the size of the browser window.

driver.manage().window().setSize(new Dimension(1024, 768));

26. manage().timeouts().implicitlyWait(Duration timeout)

 Use: Sets an implicit wait, which applies globally for all element searches.

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

27. manage().timeouts().pageLoadTimeout(Duration timeout)

 Use: Sets the maximum time to wait for a page to load.

driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(30));

28. manage().timeouts().scriptTimeout(Duration timeout)

 Use: Sets the time limit for asynchronous scripts to finish execution.

driver.manage().timeouts().scriptTimeout(Duration.ofSeconds(10));

29. switchTo().frame(int index)

 Use: Switches the focus to a frame by its index.

driver.switchTo().frame(0);
30. switchTo().frame(String nameOrId)

 Use: Switches to a frame by name or ID.

driver.switchTo().frame("frameName");

31. switchTo().frame(WebElement frameElement)

 Use: Switches to a frame using a WebElement.

WebElement frameElement = driver.findElement(By.id("frameId"));

driver.switchTo().frame(frameElement);

32. switchTo().defaultContent()

 Use: Switches back to the main content from a frame.

driver.switchTo().defaultContent();

33. switchTo().alert()

 Use: Switches to an alert dialog box.

Alert alert = driver.switchTo().alert();

alert.accept();

34. alert.accept()

 Use: Accepts the alert dialog (clicks "OK").

driver.switchTo().alert().accept();

35. alert.dismiss()

 Use: Dismisses the alert dialog (clicks "Cancel").

driver.switchTo().alert().dismiss();

36. alert.getText()

 Use: Retrieves the message from the alert dialog.

String alertMessage = driver.switchTo().alert().getText();

37. alert.sendKeys(String keysToSend)


 Use: Sends input to a prompt alert.

driver.switchTo().alert().sendKeys("input text");

38. getWindowHandle()

 Use: Retrieves the current window's handle (unique identifier).

String windowHandle = driver.getWindowHandle();

39. getWindowHandles()

 Use: Retrieves a set of all window handles.

Set<String> windowHandles = driver.getWindowHandles();

40. switchTo().window(String handle)

 Use: Switches to a window using its handle.

driver.switchTo().window(windowHandle);

You might also like