I'm trying to click on some pseudo elements using Selenium Java. According to some researches I know that Selenium doesn't support direct clicks on those elements, so I have to use JavascriptExecutor for that purpose.
Problem is it works well when I tried on the browser console, but not in my Selenium Java program. Below is what I'm trying to do.
I want to click on the highlighted arrow element
End then I want to click on the last element (test)
Here is what happens when I use JavascriptExecutor in Selenium.
The list of elements is displayed in the wrong location
I got the error : javascript error: Cannot read properties of null (reading 'click') when trying to click on the element test
Below is the code I'm using in Selenium:
public void JSclick2(String cssPath, String pseudo, String description)
throws InterruptedException, IOException, UnsupportedAudioFileException, LineUnavailableException {
// Perform click action on the element
try {
JavascriptExecutor js = (JavascriptExecutor) driver;
String JScommand = "document.querySelector('"+ cssPath + "', '" + pseudo + "').click()";
System.out.println(JScommand);
js.executeScript(JScommand);
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
printMessage(description);
}
}
Pls tell me why the problem happens and how to fix it. Thanks very much!