Selenium web elements interaction notes

Jen
3 min readNov 20, 2021

A good design of a webpage is definitely helpful for locating web elements and accessing the data from it. However, when things don’t work as we expect, there are some other solutions to achieve the same goals. Here are some notes that I took recently when handling the interaction with the web elements using Selenium in Java:

1. WebDriver object is inited by Test class and is passed to Page class

That is to say, the page object model lets the page object only handle the elements and actions of the page itself.

2. Aware driver object becomes null in POM model

This might happen when the test cases or other objects use the page but forget to create the driver object and pass it to the page object.

3. Using OOP principle to design page classes

For example, try to move common things in a page class into a PageBase class, and create a page class that extends the PageBase class.

4. Aware that invisible elements cannot be found when using WebDriverWait ExpectedConditions.visibilityOfElementLocated

For example, need to check where the input element is visible or not when sending a key to the file upload input tag for file uploading.

5. When cannot locate elements

  • Check if there are multiple windows by using Set<String> s = driver.getWindowHandles();, and switch to the target window where the element is located.
  • Check if the element is visible/ interactable

6. All the action methods have a return value

Return current page or another page which should be directed to after the action is done.

7. Locate elements inside a table

For example, when cannot locate and click an element in the table row or column, can try to click on the parent element which contains the target element.

8. When fail to locate elements

When failing to locate an element with a certain attribute (eg, class), try to locate by using different locating methods (For example, use By.cssSelector, By.className, or By.xPath)

For example, locate button element:

This does not work:

private By applyBtn = By.className("applyBtn btn btn-sm btn-glow inverse");

But this works:

private By applyBtn = By.cssSelector("button[class='applyBtn btn btn-sm btn-glow inverse']");

9. Handling Session timeout when obtaining data from the element for a too long time

Redesign the test (interaction) steps, and probably include login action into the flow.

10. Scrolling to the end of the page

// navigate to the page
driver.navigate().to(pageUrl);
// send js to scroll to end
((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");

If it does not work, can try to scroll to the element which is located at the page end using Scrolling to element.

11. Scrolling to element

If cannot scroll to certain elements, try to scroll to other elements on the same page, and return that element. And then use XPath Axes to locate the target element.

Most common way:

WebElement element = driver.findElement(By.id("id_of_element"));
driver.executeScript("arguments[0].scrollIntoView(true);", element);

Another way that might work, refer to: https://medium.com/geekculture/best-way-to-use-scrollintoview-in-selenium-using-javascriptexecutor-f53518c5beed

WebElement element = driver.findElement(By.id("id_of_element"));
String script = "arguments[0].scrollIntoView({behavior: \"auto\", block: \"center\", inline: \"center\"});";
driver.executeScript(script, element);

12. Handling Select element (dropdown list)

Select an item in a Multiple Select element.

  • A solution when the webpage is well designed: https://www.guru99.com/select-option-dropdown-selenium-webdriver.html
  • When the general approach does not work, or the Select element is not interactable (display: none): click on the parent element of it, and maybe add Thread.sleep to force sleep to wait until the action happens (But try to avoid Thread.sleep if it is possible)

For example:

Wait visible and with delay in between to make sure it stays.

public Pagination choosePerPageDisplay(final int displayCount) {
waitAndClick(perPageSelectSpan);
Thread.sleep(oneSecond);
getElementRemoveAttribute(waitVisible(by100), "unselectable").click();
return this;
}

For example:

Remove the attribute of the element

protected WebElement getElementRemoveAttribute(final WebElement element, final String attribute) {
((JavascriptExecutor) driver)
.executeScript("arguments[0].removeAttribute('" + attribute + "'); return arguments[0];", element);
return element;
}

--

--

Jen

SQA engineer specializing in test automation. Spending free time reading software testing articles and learning tools, bouldering, biking, or playing squash.