March 8, 2022

Fixing javascript error: Cannot read properties of null (reading 'querySelector') issue when using CSS identifier in selenium

  March 8, 2022

 JavaScript is another way to interact with web elements when normal selenium methods fail to act.

But one issue with javascript is, it does not throw any error if the action is not done.

Still, it throws an error as below, if we try to find the element using CSS path and if the script is not able to find it

OpenQA.Selenium.WebDriverException: javascript error: Cannot read properties of null (reading 'querySelector') 

If the element is still there we can simply solve this by adding some more wait after the page is loaded.

Unlike selenium commands, javascript will not wait for any element to be visible or interactable, So adding some appropriate wait will solve the issue.

A sample element click using JavaScript is given below.

driver.Navigate().GoToUrl("https://www.getsetautomate.com/");

Thread.Sleep(3000);

IJavaScriptExecutor executer = (IJavaScriptExecutor)driver;

string exescript = "return document.querySelector('gsc-search-button')";

IWebElement buttonToClick = (IWebElement)executer.ExecuteScript(exescript);

buttonToClick.Click();

logoblog

Thanks for reading Fixing javascript error: Cannot read properties of null (reading 'querySelector') issue when using CSS identifier in selenium

Previous
« Prev Post

No comments:

Post a Comment

How to send email from current Outlook instance using Java + VBScript

If we are using .Net, we can easily access outlook and send emails using its own libraries. But what if we are using Java? Here we can seek ...