当我试图在我的selenium代码中执行java脚本时,我得到了以下错误。
错误:响应消息: javax.script.ScriptException:源文件:内联评估: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:导入org.openqa.selenium.By;导入org.openqa.selenium.WebDriver;导入组织。。。。'‘:executor .executeScript (“document.getElementById(’.click-2‘).click()”)在内联求值中:导入org.openqa.selenium.By;导入org.openqa.selenium.WebDriver;导入组织。。。。'‘在第47行
响应头部:
代码:
public HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.CHROME,true);
JavascriptExecutor executor = (JavascriptExecutor)driver;
driver.get(appURL);
executor.executeScript("document.getElementById('icon-2')[0].click()");如何在selenium中执行java脚本?
页面dom结构:

发布于 2018-08-06 13:30:43
似乎Beanshell解释器不支持varargs,因此您尝试调用第二个参数为java.lang.Object... args的executeScript() method是失败的。
最简单的解决方案是使用Groovy语言,如下所示:
上有自己的syntax sugar。
https://stackoverflow.com/questions/51691897
复制相似问题