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();
No comments:
Post a Comment