我注意到了一些奇怪的事情。如果我使用 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