Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

phantomjs via watir-webdriver, not handling javascript alerts

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.

like image 521
user137369 Avatar asked Oct 01 '13 20:10

user137369


1 Answers

There's a known issue where PhantomJS doesn't handle JavaScript properly. One workaround is to disable the alert box by running:

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

Basically, the steps you want to follow are:

  1. Go to the page
  2. Run the command above
  3. Click 'Delete' (or whatever that triggers the alertbox)
  4. And it will automatically click 'Ok' for you.
like image 198
Major Avatar answered Nov 13 '22 17:11

Major