Quantcast
Channel: Pochampalli IT Labs
Viewing all articles
Browse latest Browse all 261

Locators for selenium | className locator example | gmail login automation example in java-selenium

$
0
0
Hi,

In this post, you will see demonstration of "className" locator usage. 

"classname" is one of the 8 locators supported by selenium, using it one can navigate to target page by performing click action(s).

For instance, Login to gmail  @ https://mail.google.com/
Let's see how to identify the name locators of login elements for gmail web application.

Email or Phone input text input HTML with "name" locator 
<inputtype="email"class="whsOnd zHQkBf"jsname="YPqjbf"autocomplete="username"spellcheck="false"tabindex="0"aria-label="Email or phone"name="identifier"value=""autocapitalize="none"id="identifierId"dir="ltr"data-initial-dir="ltr"data-initial-value=""badinput="false"aria-invalid="false"xpath="1">

selenium identifies the above input element(Email or Phone) using "className" locator with the following java statement. 
driver.findElement(By.className("whsOnd")).sendKeys("java.selenium2021@gmail.com");

Tap on the image to get better visibility: 
To avoid StaleElementReferenceException for other elements locators having the same class name ,  I am taking xpaths to find them, for instance , password has the same className i.e., class="whsOnd zHQkBf"  so instead of having className for password taking xpath //input[@name='password']


classNameLocatorDemo.java
package selenium.locators.examples;

importjava.util.concurrent.TimeUnit;
importorg.openqa.selenium.By;
importorg.openqa.selenium.Keys;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.WebElement;
importorg.openqa.selenium.chrome.ChromeDriver;

publicclassclassNameLocatorDemo{

publicstaticvoidmain(String[] args){

WebDriver driver
;

System
.setProperty("webdriver.chrome.driver","D:\\006_trainings\\chromedriver.exe");
System
.setProperty("webdriver.chrome.silentOutput","true");

driver
=new ChromeDriver();

driver
.navigate().to("https://mail.google.com/");
driver
.manage().window().maximize();

//finding "Email or Phone" input text by clasName locator and enter value
driver
.findElement(By.className("whsOnd")).sendKeys("java.selenium2021@gmail.com");

// click on "Next" button - This is an xpath example that will be covered in later sessions
driver
.findElement(By.xpath("//span[@class='RveJvd snByac']")).click();

driver
.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

// locate "Enter your password" text input with xpath locator and enter password say --> JavaSelenium2021
// If we take className for this input we will end up with
// Exception in thread "main" org.openqa.selenium.StaleElementReferenceException:
// stale element reference: element is not attached to the page document

WebElement elePassword
=driver.findElement(By.xpath("//input[@name='password']"));
elePassword
.sendKeys("JavaSelenium2021");

elePassword
.sendKeys(Keys.TAB);
elePassword
.sendKeys(Keys.TAB);

// click on "Next" button - This is again an xpath example.
driver
.findElement(By.xpath("//span[contains(text(),'Next')]")).click();

//close the driver
//driver.close();
}
}



Viewing all articles
Browse latest Browse all 261

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>