【问题标题】:executescript not working with InternetExplorer driver (Selenium)执行脚本不能与 InternetExplorer 驱动程序 (Selenium) 一起使用
【发布时间】:2013-12-31 18:17:10
【问题描述】:

由于某种原因,当我通过 IE 运行功能测试时,我无法使用“executescript”执行任何 javascript。它适用于火狐。我在谷歌上搜索了几个小时没有运气。

我只是在打电话

browser.driver.executeScript("console.log('test');")

JavascriptExecutor js = (JavascriptExecutor) driver
driver.executeScript("console.log('test');")

或任何你喜欢的变体来调用 executeScript 方法。

我得到的堆栈跟踪是:

org.openqa.selenium.WebDriverException: JavaScript error (WARNING:
The server did not provide any stacktrace information)
Command duration or timeout: 164 milliseconds
Build info: version: '2.37.1', revision: 'a7c61cbd68657e133ae96672cf995890bad2ee42',           
time: '2013-10-21 09:08:07'
System info: host: 'functionalTests', ip: '10.22.6.112', os.name: 'Windows 8', os.arch:    
'x86', os.version: '6.2', java.version: '1.6.0_45'
Session ID: 8b04c740-07a0-4678-a1b6-aacd56268625
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{platform=WINDOWS, javascriptEnabled=true, elementScrollBehavior=0,     
enablePersistentHover=true, ignoreZoomSetting=false, ie.ensureCleanSession=false,   
browserName=internet explorer, enableElementCacheCleanup=true,   
unexpectedAlertBehaviour=dismiss, version=11, ie.usePerProcessProxy=false, 
cssSelectorsEnabled=true, ignoreProtectedModeSettings=false, requireWindowFocus=false,  
initialBrowserUrl=http://localhost:17553/, handlesAlerts=true,  
ie.forceCreateProcessApi=false, nativeEvents=true, browserAttachTimeout=0, 
ie.browserCommandLineSwitches=, takesScreenshot=true}]
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)
at  
org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
at 
org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:463)
at functional.utility.JQInstaller.installJQ(JQInstaller.groovy:16)
at functional.spec.NavigateAccountSettingsSpec.Navigate to the Account 
Settings(NavigateAccountSettingsSpec.groovy:39)

我的“JQInstaller”类没有问题,因为我把它拿出来并尝试了 console.log 功能。这个“executeScript”方法有问题。我正在使用 Selenium 2.37.1。

【问题讨论】:

  • 你用的是什么版本的IE? IE8及以下没有console.log
  • 我使用的是 9,10 和 11
  • @elgalu,我们在该页面上没有丢失的资源。太糟糕了,我希望就是这样。
  • 我有同样的问题,使用 Selenium 2.40.0,我报告了它:code.google.com/p/selenium/issues/…
  • 您说“没有任何效果”,但如果 F12 开发人员工具窗口未打开,您的示例将出错(如果 F12 工具未打开,则访问“console.”会在至少 IE js.executeScript("return 1+1;"); 之类的操作会发生什么?

标签: javascript internet-explorer selenium geb


【解决方案1】:

你试过 Enumerable 版本吗?

IJavaScriptExecutor js = WebDriver.driver as IJavaScriptExecutor;

if (js != null) {
     value = (string)js.ExecuteScript(javascriptArgumentAsString, element);
}

【讨论】:

  • 对不起,这是一个get,这是集合;
  • IJavaScriptExecutor js = WebDriver.driver 作为 IJavaScriptExecutor; if (js != null) { js.ExecuteScript(javascriptArgumentStringSet(attribute, value), element); }
  • 实际上,我认为我可以看到问题。当它应该是 js.ExecuteScript 时,您正在执行 driver.ExecuteScript。
【解决方案2】:

IE 7 及以下版本没有 console.log。

IE 8、9 和 10 都有,但必须打开开发工具。用 F12 打开这些。

IE 11 运行良好。

每个 IE 浏览器都有一个脚本调试窗口,可以在 IE 工具菜单中激活。这会导致提醒您在旧版 IE 浏览器中失败的原因。

如果您需要额外的帮助来理解这个外观:Specifically at Walter's answer for an excellent solution

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多