Member-only story

Cannot find/click/type in element? Use Selenium, Java and ……….. Javascript

Alex Siminiuc
6 min readApr 26, 2020

--

Photo by Obi Onyeador on Unsplash

Sometimes, you try finding an element with Selenium WebDriver and it does not work.

The locator of the element is correct. The element is not included in an iframe. The element is visible and seems enabled.

Still, you cannot find it or interact with it through the Selenium API.

In these cases, you should rely on Javascript code instead.

A quick example shows how.

The following Selenium test implements a simple test case:

  • open the home page of the site (vpl.ca)
  • check that the home page title and url are correct
  • execute a keyword search
  • check that the results page title and url are correct
  • on results page, check that the page count (1 to 10 of 24 results) from the top of the page is equal to the page count from the bottom of the page
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class JavascriptTests1 {

WebDriver driver;

By…

--

--

Alex Siminiuc
Alex Siminiuc

No responses yet