How to Get Element with Selenium Webdriver
In order to interact with the UI of a web application, we need to have a WebElement object.
Here’s a (very) simple example of getting an element from “google.com”:
- Navigate to www.google.com
- Right click + inspect element
- Find a unique identifier (id/class name)
- Use selenium “findElement(By)”
WebDriver driver;
@Test
public void getElementTest()
{
driver = new ChromeDriver();
driver.get("http://wwww.google.com");
WebElement element = driver.findElement(By.className("RNNXgb"));
}
POSSIBLE PROBLEMS WITH THE CODE ABOVE
The code isn’t generic and not reusable.
The default Selenium design will throw an exception, which will fail your test
What happens in case the element is not found?
The default Selenium design will throw an exception, which will fail your test.
Is it good? I personally don’t think that basing your tests on possible exceptions is a good practice, that’s why “We got to take the power back”
POSSIBLE SOLUTIONS TO THE CODE ABOVE
First, we will move our code to a seperate method, so we would be able to access it from multiple tests:
public WebElement getElementByClassName(String className)
{
WebElement element;
try
{
element = driver.findElement(By.className(className));
}
catch (Exception e)
{
element = null;
}
return element;
}
Explanation:
Our method is using a “try” and “catch” blocks. Why? As was mentioned earlier, in case the element wasn’t found, Selenium will throw an exception which will fail our test. By enforcing a return value, we can decide in our test, what to do next: fail the test, try again, try a different identifier or whatever
@Test
public void getElementTest()
{
driver = new ChromeDriver();
driver.get("http://wwww.google.com");
WebElement element = this.getElementByClassName("RNNXgb");
}
As you can see, we’re now using a reference to our new method and we don’t need to be afraid of any exception.
Let’s say now that we want to use an “id”. We can write the same method for using an id:
public WebElement getElementById(String id)
{
WebElement element;
try
{
element = driver.findElement(By.id(id));
}
catch (Exception e)
{
element = null;
}
return element;
}
Voila! but… wait! Did you recognize a problem with our code?
Yes, you are right: Both methods are practically the same beside the identifier. It means that we now need to maintain two methods with similar characteristics separately.
What can we do? We can merge them into one “parent” method:
public WebElement getElementBy(By by)
{
WebElement element;
try
{
element = driver.findElement(by);
}
catch (Exception e)
{
element = null;
}
return element;
}
Now, this method is handling the exception, and all we need to do is to access it from our previous methods:
public WebElement getElementByClassName(String className)
{
return this.getElementBy(By.className(className));
}
public WebElement getElementById(String id)
{
return this.getElementBy(By.id(id));
}
Needless to say that we don’t need to change anything in our test. For the purpose of this example we will fail the test in case the element is null:
@Test
public void getElementTest()
{
driver = new ChromeDriver();
driver.get("http://wwww.google.com");
WebElement element = this.getElementByClassName("RNNXgb");
Assert.assertNotNull(element,"The element is null, failing test!");
}
Below you can check out our video tutorial for this article:
In the next article we’ll explain how to get sub element with Selenium.
Enjoy!