7

I’m trying to automate some tasks on a webpage, and part of it includes clicking a link that’ll show a javascript alert where you have to press “OK”. Trouble is, when it gets to that point, it fails with the following error

/Users/username/.gem/ruby/2.0.0/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/remote/http/common.rb:66:in `create_response': unexpected response, code=405, content-type="text/plain" (Selenium::WebDriver::Error::WebDriverError)
Invalid Command Method - Request => {"headers":{"Accept":"application/json","Accept-Encoding":"gzip;q=1.0,deflate;q=0.6,identity;q=0.3","Cache-Control":"no-cache","Connection":"close","Host":"127.0.0.1:8910","User-Agent":"Ruby"},"httpVersion":"1.1","method":"GET","url":"/alert_text","urlParsed":{"anchor":"","query":"","file":"alert_text","directory":"/","path":"/alert_text","relative":"/alert_text","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/alert_text","queryKey":{},"chunks":["alert_text"]},"urlOriginal":"/session/6772bf50-2ad9-11e3-86e7-55e618e30fa2/alert_text"}
    from /Users/username/.gem/ruby/2.0.0/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/remote/http/default.rb:66:in `request'
    from /Users/username/.gem/ruby/2.0.0/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/remote/http/common.rb:40:in `call'
    from /Users/username/.gem/ruby/2.0.0/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/remote/bridge.rb:634:in `raw_execute'
    from /Users/username/.gem/ruby/2.0.0/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/remote/bridge.rb:612:in `execute'
    from /Users/username/.gem/ruby/2.0.0/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/remote/bridge.rb:150:in `getAlertText'
    from /Users/username/.gem/ruby/2.0.0/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/common/alert.rb:9:in `initialize'
    from /Users/username/.gem/ruby/2.0.0/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/common/target_locator.rb:76:in `new'
    from /Users/username/.gem/ruby/2.0.0/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/common/target_locator.rb:76:in `alert'
    from /Users/username/.gem/ruby/2.0.0/gems/watir-webdriver-0.6.4/lib/watir-webdriver/alert.rb:93:in `assert_exists'
    from /Users/username/.gem/ruby/2.0.0/gems/watir-webdriver-0.6.4/lib/watir-webdriver/alert.rb:37:in `ok'
    from ./olx:21:in `block in <main>'
    from ./olx:19:in `times'
    from ./olx:19:in `<main>'

Searching online, it seems like the trouble may be related to ghostdriver and that maybe phantomjs can’t do it, but that notion seems a bit weird to me (it’s called phantom*js*, after all, and an alert is pretty basic javascript), but maybe I’m looking at it wrong.

Running the script with chrome, it works flawlessly.

4

2 回答 2

11

有一个已知问题是 PhantomJS 无法正确处理 JavaScript。一种解决方法是通过运行以下命令禁用警报框:

@browser.execute_script("window.confirm = function(){return true;}");

基本上,您要遵循的步骤是:

  1. 转到页面
  2. 运行上面的命令
  3. 单击“删除”(或触发警报框的任何内容)
  4. 它会自动为您单击“确定”。
于 2013-10-15T13:52:11.840 回答
-1

当然,

在开始行动之前运行它:

browser.execute_script("window.confirm = function(){return true;}");

它有助于确认弹出窗口。

于 2017-03-15T15:16:38.033 回答