1

我注意到了一些奇怪的事情。如果我使用 Firefox,这可以正常工作:

require "selenium-webdriver"
# => true 

driver = Selenium::WebDriver.for :firefox
# => #<Selenium::WebDriver::Driver:0x..fc3c6738535bdb898 browser=:firefox> 

driver.execute_script "window.alert('text');"
# => "text" 

警报弹出窗口打开。

如果我对 Chrome 执行相同操作,则会打开警报弹出窗口,但出现异常:

driver = Selenium::WebDriver.for :chrome
# => #<Selenium::WebDriver::Driver:0x47f3180ec45394e8 browser=:chrome> 

driver.execute_script "window.alert('text');"
Selenium::WebDriver::Error::UnhandledAlertError: Script execution failed. Script: window.alert('text');;
 An open modal dialog blocked the operation
    from /Users/zeljko/.rvm/gems/ruby-1.9.2-p290@gretel/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/remote/response.rb:52:in `assert_ok'
    from /Users/zeljko/.rvm/gems/ruby-1.9.2-p290@gretel/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/remote/response.rb:15:in `initialize'
    from /Users/zeljko/.rvm/gems/ruby-1.9.2-p290@gretel/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/remote/http/common.rb:59:in `new'
    from /Users/zeljko/.rvm/gems/ruby-1.9.2-p290@gretel/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/remote/http/common.rb:59:in `create_response'
    from /Users/zeljko/.rvm/gems/ruby-1.9.2-p290@gretel/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/remote/http/default.rb:64:in `request'
    from /Users/zeljko/.rvm/gems/ruby-1.9.2-p290@gretel/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/remote/http/common.rb:40:in `call'
    from /Users/zeljko/.rvm/gems/ruby-1.9.2-p290@gretel/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/remote/bridge.rb:590:in `raw_execute'
    from /Users/zeljko/.rvm/gems/ruby-1.9.2-p290@gretel/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/remote/bridge.rb:568:in `execute'
    from /Users/zeljko/.rvm/gems/ruby-1.9.2-p290@gretel/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/remote/bridge.rb:314:in `executeScript'
    from /Users/zeljko/.rvm/gems/ruby-1.9.2-p290@gretel/gems/selenium-webdriver-2.19.0/lib/selenium/webdriver/common/driver.rb:209:in `execute_script'
    from (irb):14
    from /Users/zeljko/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'

我在这里做错了吗?

环境:

  • Mac OS X 10.7.3
  • 火狐10.0.2
  • 铬 17.0.963.56
  • chromedriver_mac_18.0.1022.0
  • 红宝石红宝石 1.9.2p290
  • 硒网络驱动程序 2.19.0
4

1 回答 1

0

使用 chromedriver 再试一次。Java 的示例:

ChromeDriver driver = new ChromeDriver();

driver.executeAsyncScript("arguments[0](); alert('Hello')");
try {
    Thread.sleep(3000);
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
于 2012-05-24T06:02:50.187 回答