Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

270
Views
Javascript works well on the browser console but not in Selenium JavascriptExecutor

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!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!