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

Bookmark this website for more workarounds and issue fixes.

Verify Zip file contents without extracting using C# + Selenium

While doing automation testing, we may get a scenario where we need to download a zip file and to validate the contents inside it. One way t...