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"))

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:



<div class="thumbnail center well well-small text-center">


<h2>Newsletter</h2>


   Subscribe to our weekly Newsletter and stay tuned.


<form action="" method="post" name="subscribe"><label for="name">Name: </label>
      <input class="name" id="name" type="text" placeholder="Enter name..." />
      <label for="email">Email: </label> <input class="email" id="email" type="text" placeholder="your@email.com" />
      <input class="btn btn-large" type="submit" value="Subscribe" />
   </form>


   <a title="first link" href="#link1">First Link</a>
   <a title="second link" href="#link2">Second Link</a>
</div>


Let’s get some WebElements from the above HTML code snippet:

 WebElement nameInputField = driver.findElement(By.id("name"));

Related Posts

Check out how to take a screenshot with Selenium WebDriver or to pass traffic though a proxy.

Check also The Ultimate Selenium WebDriver Testing & Hands-on Guide – Online Course.