Element-locating functions are the building blocks of Selenium tests. These methods handle Ajax calls using timeouts and wait conditions to search and locate elements within a web page. There is no fixed strategy that you can follow to locate an element; it depends on the user ideology and their comfort level in locating the web elements.
An element can be located using any kind of locator type. Selenium WebDriver uses locators to interact with elements present in a web page. The following is the list of locator types used in Selenium WebDriver to locate web elements:
By.id
By.name
By.xpath
By.cssSelector
By.className
By.linkText
By.tagName
By.partialLinkText
The following is the syntax to use locatorType
:
driver.findElement(By.locatorType("path"))
The following is an example for locatorType
:
driver.findElement(By.id("sblsbb")).click();
Prioritize the locator type in this order: id > name > css > xpath
. The cssSelector
locator type is a good pick to work with Ajax calls. Always try to avoid locating elements using XPath locators. Remember that Internet Explorer tests often fail to respond to XPath locators. The XPath locator type is of two categories, namely, absolute XPath /
and relative XPath //
. Consider an element on the Google search page:
Absolute XPath |
Relative XPath |
---|---|
|
|
Here, both the absolute and relative XPath point to the same element.
Let's take a look at Selenium WebDriver's element-locating functions, which are as follows:
findElement()
: This element locates the first element within the current page. The following is the syntax for this function:driver.findElement(By.locatorType("path"));
The following is an example where Selenium WebDriver locates the Google search text field using the findElement()
method:
driver.get("https://www.google.com"); WebElement element = driver.findElement(By.id("lst-ib"));
findElements()
: This element locates all the elements within the current page. The following is the syntax for this function:List<WebElement> elements = driver.findElements(By.locatorType("path")); WebElement element = driver.findElement(By.locatorType("path")); List<WebElement> elements = element.findElements(By.locatorType("path"));
Some of the useful tasks that you can perform with the help of these functions are as follows:
driver.get("https://accounts.google.com/"); List<WebElement> Textbox =driver.findElements(By.xpath("//script[@type='text/javascript']")); System.out.println("Overall textboxes:"+Textbox.size());
driver.get("http://www.indiabookstore.net"); driver.findElement(By.id("searchBox")).sendKeys("Alche"); List <WebElement> listItems = driver.findElements(By.xpath("//div[3]/ul/li")); listItems.get(0).click();
tagName
function:List<WebElement> link = driver.findElement(By.locatorType("path")).findElements(By.tagName("li"));
findElements()
method is used to return the total length of the option tag, and it lets you pick all the preferred checkboxes:driver.get("http://www.ryancramer.com/journal/entries/select_multiple/"); List<WebElement> ele = driver.findElements(By.tagName("select")); System.out.println(ele.size()); WebElement ele2 = ele.get(0); List<WebElement> ele3 = ele2.findElements(By.tagName("option")); System.out.println(ele3.size()); ele2.sendKeys(Keys.CONTROL); ele3.get(0).click(); ele3.get(1).click(); ele3.get(3).click(); ele3.get(4).click(); ele3.get(5).click();
findElements()
method. Later, the user is intended to navigate each and every link one by one. The following snippet is an example for capturing and navigating all links in a web page:private static String[] links = null; private static int linksCount = 0; driver.get("https://www.google.co.in"); List<WebElement> linksize = driver.findElements(By.tagName("a")); linksCount = linksize.size(); System.out.println("Total no of links Available: "+linksCount); links= new String[linksCount]; System.out.println("List of links Available: "); // print all the links from webpage for(int i=0;i<linksCount;i++) { links[i] = linksize.get(i).getAttribute("href"); } // navigate to each Link on the webpage for(int i=0;i<linksCount;i++) { driver.navigate().to(links[i]); Thread.sleep(3000); }
StaleElementException
will be thrown whenever a user navigates through the link and tries to click on the second link after returning from the visited page. To avoid such exceptions, an external method, getElementWithIndex()
, is used to return values. The following code snippet exemplifies this method:driver.get("https://www.google.co.in");
WebElement element = driver.findElement(By.locatorType("path"));
List<WebElement> elements = element.findElements(By.tagName("a"));
int sizeOfAllLinks = elements.size();
System.out.println(sizeOfAllLinks);
for(int i=0; i<sizeOfAllLinks ;i++)
{
System.out.println(elements.get(i).getAttribute("href"));
}
for (int index=0; index<sizeOfAllLinks; index++ ) {
getElementWithIndex(By.tagName("a"), index).click();
driver.navigate().back();
}
public WebElement getElementWithIndex(By by, int index) {
WebElement element = driver.findElement(By.id(Value));
List<WebElement> elements = element.findElements(By.tagName("a"));
return elements.get(index);
}
driver.get("https://www.google.co.in"); List<WebElement> all_links_webpage = driver.findElements(By.tagName("a")); System.out.println("Total no of links Available: " + all_links_webpage.size()); int k = all_links_webpage.size(); System.out.println("List of links Available: "); for(int i=0;i<k;i++) { if(all_links_webpage.get(i).getAttribute("href") .contains("google")) { String link = all_links_webpage.get(i).getAttribute("href"); System.out.println(link); } }
<li>
tag:driver.get("http://www.indiabookstore.net"); driver.findElement(By.id("searchBox")).sendKeys("Alche"); Thread.sleep(3000); List <WebElement> listItems = driver.findElements(By.cssSelector(".acResults li")); listItems.get(0).click(); driver.findElement(By.id("searchButton")).click();
driver.get("http://www.indiabookstore.net/"); driver.findElement(By.id("searchBox")).sendKeys("sam"); WebElement table = driver.findElement(By.className("acResults")); List<WebElement> rowlist = table.findElements(By.tagName("li")); System.out.println("Total No. of list: "+rowlist.size()); Iterator<WebElement> i = rowlist.iterator(); System.out.println("Storing Auto-suggest.........."); while(i.hasNext()) { WebElement element = i.next(); System.out.println(element.getText()); }
temp
. Through temp
, the values are being retrieved:List<WebElement> listitems = driver.findElements(By.id("value")); for(WebElement temp: listitems) // temp is the declared variable name { System.out.println((temp.findElement(By.tagName("value")).getText())); }