Selenium WebDriver Methods

Selenium WebDriver Methods, Actions on web elements, Selenium Brower commands, browser navigation commands, and commands on web elements.

Element Locators – Locating/Identifying/Recognizing elements in web pages
Selenium WebDriver methods – To perform actions on web elements

Note: Using Element Locators and WebDriver methods we create Test steps.

Selenium WebDriver API Commands

i. Methods on Browser

1. get(arg/url) – Argument is required and it doesn’t return any value
2. getTitle() – No Argument is required but it returns a value
3. getCurretUrl() – No Argument is required but it returns a value
4. manage() – No Argument and no return value
5. navigate()
.to(arg) – Argument is required and it doesn’t return any value
.back() – No Argument and no return value
.forward() – No Argument and no return value
.refresh() – No Argument and no return value
6. close() – No Argument and no return value
7. quit() – No Argument and no return value

ii. Methods on web elements

8. finElement(arg) – Argument is required and ……
9. findElements(arg) – Argument is required and ……
10. sendKeys(arg) – Argument is required and it doesn’t return any value
11. click() – No Argument and no return value
12. clear() – No Argument and no return value
13. getText() – No Argument is required but it returns a value
14. getAttribute(arg) – Argument is required and it returns any value

iii. Verification methods on web elements

15. isDisplayed() – No Argument is required but it returns a value
16. isEnabled() – No Argument is required but it returns a value
17. isSelecctd() No Argument is required but it returns a value

Selenium WebDriver Methods


Actions on Web/HTML Elements

1. get(arg/url);

Description:
It opens/launches a specified URL in the Browser window

Syntax:
driverObject.get(url);

Example:
driver.get(“https://gcreddy.com/project/create_account.php”);

2. getTitle()

Description: It returns the current browser title

Syntax:
String variable = driverObject.getTitle();

Example:
driver.get(“https://gcreddy.com/project/create_account.php”);
String pageTitle = driver.getTitle();
System.out.println(pageTitle);

3. getCurrentUrl()

Description: It returns the current browser URL

Syntax:
String variable = driverObject.getCurrentUrl();

Example:

driver.get(“https://gcreddy.com/project/create_account.php”);
String pageURL = driver.getCurrentUrl();
System.out.println(pageURL);

4. close()

Description: It closes the focused browser window

Syntax:
driverObject.close();

Example:
driver.get(“https://gcreddy.com/project/create_account.php”);
Thread.sleep(3000);
driver.close();

5. quit()

Description:
It closes the all browsers that opened by Selenium WebDriver during execution.

Syntax:
driverObject.qit();

Example:
driver.get(“https://www.gcreddy.com/2020/03/software-testing-project.html”);
driver.findElement(By.partialLinkText(“Admin”)).click();
Thread.sleep(3000);
driver.quit();

6. navigate().to(arg/url)

Description: it navigates to a specified URL

Syntax:
driverObject.navigate().to(arg/url);

example:

driver.get(“https://www.gcreddy.com/”);
Thread.sleep(2000);
driver.navigate().to(“https://www.google.com/”);

Or
driver.get(“https://www.gcreddy.com/”);
Thread.sleep(2000);
driver.get(“https://www.google.com/”);

7. navigate().back()

description: It moves a single item back in the browser history.

Syntax:
driverObject.navigate().back();

Example:
driver.get(“https://www.gcreddy.com/”);
Thread.sleep(2000);
driver.navigate().to(“https://www.google.com/”);
Thread.sleep(2000);
driver.navigate().back();

8. navigate().forward()

Description:
It moves a single item forward in the browser history.

Syntax:
driverObject.navigate().forward();

Example:

driver.get(“https://www.gcreddy.com/”);
Thread.sleep(2000);
driver.navigate().to(“https://www.google.com/”);
Thread.sleep(2000);
driver.navigate().back();
Thread.sleep(2000);
driver.navigate().forward();

9. navigate().refresh()

Description: It reloads the web page

Syntax:
driverObject.navigate().refresh();

example:

driver.get(“https://gcreddy.com/project/admin/login.php”);
driver.findElement(By.name(“username”)).sendKeys(“India12345”);
Thread.sleep(3000);
driver.navigate().refresh();

10. findElement(arg)

Description: It finds the first element within the current web page by given locator.

Syntax:
driverObject.findElement(By.ElementLocator(“value”)).WebDrivermthod();

Example:

driver.get(“https://www.google.com”);
driver.findElement(By.partialLinkText(“ma”)).click();

11. sendKeys(arg)

It enters a value into an edit box ot text box or TextArea.

Syntax:
driverObject.findElement(By.ElementLocator(“value”)).sendKeys(arg);

Example:

driver.get(“https://gcreddy.com/project/admin/login.php”);
driver.findElement(By.name(“username”)).sendKeys(“India@123”);

String Username= “India@123”;

driver.get(“https://gcreddy.com/project/admin/login.php”);
driver.findElement(By.name(“username”)).sendKeys(Username);

12. clear()

Description:
It clears the value from an edit box or text box

Syntax:
driverObject.findElement(By.ElementLocator(“value”)).clear();

Example:

System.setProperty(“webdriver.chrome.driver”, “D://chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get(“https://gcreddy.com/project/admin/login.php”);
driver.findElement(By.name(“username”)).sendKeys(“India@123”);
Thread.sleep(3000);
driver.findElement(By.name(“username”)).clear();

13. click()

Description:
It clicks Element (Button, Link, Radio Button, and Checkbox).

Button – click – click() – Submits
Link – click – click() – Redirects
Radio Button – click – click() – Selects an option
Checkbox – click – click() – Check/Uncheck

Syntax:
driverObject.findElement(By.ElementLocator(“value”)).click();

Example:

System.setProperty(“webdriver.chrome.driver”, “D://chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get(“https://gcreddy.com/project/”);
Thread.sleep(2000);

//Click on a Link Element
driver.findElement(By.linkText(“create an account”)).click();
Thread.sleep(2000);

//Click a Radio Button
driver .findElement(By.name(“gender”)).click();
Thread.sleep(2000);

//Click a Check box
driver.findElement(By.name(“newsletter”)).click();
Thread.sleep(2000);

//Click a Button
driver.findElement(By.id(“tdb4”)).click();

14. getAttribute(arg/attribute)

Description:
It returns the specified attribute value of an element.

String variable = driverObject.findElement(By.ElementLocator(“value”)).getAttribute(arg/attribute);

Example:
System.setProperty(“webdriver.chrome.driver”, “D://chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get(“https://gcreddy.com/project/admin/login.php”);

String typeofInput = driver.findElement(By.name(“username”)).getAttribute(“type”);
System.out.println(typeofInput);//text

driver.findElement(By.name(“username”)).sendKeys(“India@1234”);
String val = driver.findElement(By.name(“username”)).getAttribute(“value”);
System.out.println(val);//India@1234

typeofInput = driver.findElement(By.name(“password”)).getAttribute(“type”);
System.out.println(typeofInput);//password

val = driver.findElement(By.id(“tdb1”)).getAttribute(“type”);
System.out.println(val);//submit

driver.close();

15. getText()

Description:
it returns the ‘text’ value present on a web a page.

Syntax:
String vaiable = driverObject.findElement(By.ElementLocator(“value”)).getText();

example:
System.setProperty(“webdriver.chrome.driver”, “D://chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get(“https://gcreddy.com/project/admin/login.php”);

driver.findElement(By.id(“tdb1”)).click();
Thread.sleep(2000);

String errMessage = driver.findElement(By.className(“messageStackError”)).getText();
System.out.println(errMessage);

String pageHeading = driver.findElement(By.className(“pageHeading”)).getText();
System.out.println(pageHeading);

driver.close();

16. isDisplayed()

Description: It checks whether an element is exists/visible/present or not on a web page, and it returns boolean result.

Syntax:
boolean variable = driverObject.findElement(By.ElementLocator(“value”)).isDisplayed();

Example:

System.setProperty(“webdriver.chrome.driver”, “D://chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get(“https://www.google.com/”);

boolean linkExists = driver.findElement(By.linkText(“Gmail”)).isDisplayed();
System.out.println(linkExists);//true

driver.close();

17. isEnabled()

Description: It checks whether an element is enabled or not, on a web page, and it returns boolean result.

Syntax:
boolean variable = driverObject.findElement(By.ElementLocator(“value”)).isEnabled();

Example:

System.setProperty(“webdriver.chrome.driver”, “D://chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get(“https://www.google.com/”);

boolean linkstatus = driver.findElement(By.linkText(“Gmail”)).isEnabled();
System.out.println(linkstatus);//true

driver.close();

18. isSelected()

Description: It checks whether a Radio button or checkbox is Selected or not, on a web page, and it returns boolean result.

Syntax:
boolean variable = driverObject.findElement(By.ElementLocator(“value”)).isSelected();

Example:

System.setProperty(“webdriver.chrome.driver”, “D://chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get(“https://gcreddy.com/project/create_account.php”);

boolean status = driver.findElement(By.name(“gender”)).isSelected();
System.out.println(status);//false

driver.findElement(By.name(“gender”)).click();

status = driver.findElement(By.name(“gender”)).isSelected();
System.out.println(status);//true

boolean checkboxStatus = driver.findElement(By.name(“newsletter”)).isSelected();
System.out.println(checkboxStatus);//false

driver.findElement(By.name(“newsletter”)).click();

checkboxStatus = driver.findElement(By.name(“newsletter”)).isSelected();
System.out.println(checkboxStatus);//true

driver.close();


Manual Testing Tutorial

Java Tutorial

Python Tutorial

SQL Tutorial

Selenium, Java, and Testing Videos

Follow me on social media:

1 thought on “Selenium WebDriver Methods”

Comments are closed.