When attempting to run JavaScript in my Selenium code, I encountered the following error:
Error: Response message: javax.script.ScriptException: Sourced file: inline evaluation of:
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org. . . . '' : Typed variable declaration : Error in method invocation: Method executeScript( java.lang.String ) not found in class 'org.openqa.selenium.htmlunit.HtmlUnitDriver' : at Line: 47 : in file: inline evaluation of:
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org. . . . '' : executor .executeScript ( "document.getElementById('icon-2')[0].click()" )
in inline evaluation of: ``import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org. . . . '' at line number 47
Response headers:
Code:
public HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.CHROME,true);
JavascriptExecutor executor = (JavascriptExecutor)driver;
driver.get(appURL);
executor.executeScript("document.getElementById('icon-2')[0].click()");
What is the correct way to run JavaScript within Selenium?
Page dom structure :