How to Locate Web Elements with Selenium WebDriver?
3minutes read
Load Focus is a Cloud Load Testing Service. You can stress your app/website/API with thousands of concurrent users from all over the world.
In order to write UI tests with Selenium WebDriver you need to be able to identify web page elements fast and in an accurate way. You don’t want to revisit these selectors very often so you must choose the right selector from the beginning.
There are some browser tools that you can use in order to identify web elements in the DOM easier. These are:
Firebug for Firefox
Google Developer Tools for Chrome
Web Inspector for Safari
You can also create automated website tests for free with LoadFocus.com.
Selenium WebDriver API supports different possibilities to identify elements: by ID, by CLASS, by NAME, by CSS selector, by XPath, by TAG name. Also you define your custom selector in order to interact with the elements.
It’s always a good practice to assign unique IDs to elements, also names and classes in order to be more usable for automatic UI tests. If that is not possible you’ll need to use advanced or XPath selector to interact with those elements. The most popular selectors are the CSS selectors due to performance and simplicity reasons.
To inspect an element you just have to open the desired web page, right-click the desired element and click on Inspect Element. A new panel will open showing the desired element. Also you can inspect other elements by clicking on the cursor in the top left side of the Developer Tools or Firebug panels and hovering page elements.
Locating Elements with Selenium WebDriver, findElement() method returns and WebElement and findElements() returns a list of WebElements.
1. By ID:
in Java: driver.findElement(By.id("element id"))
2. By CLASS:
in Java: driver.findElement(By.className("element class"))
3. By NAME:
in Java: driver.findElement(By.name("element name"))
4. By TAGNAME:
in Java: driver.findElement(By.tagName("element html tag name"))
See more details
5. By CSS Selector:
in Java: driver.findElement(By.cssSelector("css selector"))
6. By Link:
in Java: driver.findElement(By.link("link text"))
7. By XPath:
in Java: driver.findElement(By.xpath("xpath expression"))
Let’s get a HTML snippet code and see how can we use these Selenium WebDriver selectors in order to identify the desired elements:
Locate link elements with findElements() method from the HTML snipped
@Test
public void findLinksTest(){
//Get all the links displayed
List links = driver.findElements(By.tagName("a"));
assertEquals(2, links.size());
for(WebElement link : links)
System.out.print(link.getAttribute("href"));
}
Locate link elements from the HTML snipped:
How to find a link with Selenium WebDriver API by full text:
Finding elements using attributes values with XPath
WebElement logo =
driver.findElement(By.xpath("img[@alt='logo']"));
XPATH
starts-with()
input[starts-with(@id,'input')]
Starting with:
ends-with()
input[ends-with(@id,'_field')]
Ending with:
contains()
input[contains(@id,'field')]
Containing
Locating table rows and cells
@Test
public void testTable() {
WebElement simpleTable = driver.findElement(By.id("items"));
//Get all rows
List rows = simpleTable.findElements(By.tagName("tr"));
assertEquals(3, rows.size());
//Print data from each row
for (WebElement row : rows) {
List cols = row.findElements(By.tagName("td"));
for (WebElement col : cols) {
System.out.print(col.getText() + "\t");
}
System.out.print();
}
}
Using jQuery selectors
Locate all the Checkbox which are checked by calling jQuery find() method. find() method returns elements in array
List elements =
(List) js.executeScript("return jQuery.find(':checked')");
Get the text of an element with no class or id, and dynamic rendering inside the DOM:
For example:
Text1
This is the method to return the text of an WebElement based on the parent element:
public String getCity(){
WebElement element = driver.findElement(By.xpath("//img[contains(@src,'/image1.png')]/parent::*"));
return element.getText();
}