phantomjs via watir-webdriver, not handling javascript alerts
Asked Answered
D

2

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.

Diddle answered 1/10, 2013 at 20:51 Comment(6)
Check this and let me know #11151722Whitaker
I still get the same error.Diddle
Would you mind capture a screenshot to see if the alert box is there?Saree
Nope, the alert box is not visible on the screenshot.Diddle
Could the solution provided here help: #9216213?Ontiveros
No, that also does nothing.Diddle
N
11

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.
Nickynico answered 15/10, 2013 at 13:52 Comment(4)
How can something have js in the name and handle javascript poorly? Thank you very much, I had a bounty on this that already expired, I didn’t think I’d get an answer. Is there anyway I can further repay you for the answer?Diddle
I didn't understand your question clearly. Can you rephrase your question?Nickynico
I was just venting on the first question — phantomjs has js in the name (for javascript), so it’s a bit ironic that it doesn’t handle javascript properly. The idea is that it is scriptable with javascript, yes, but still. Regarding the second question, is there anyway I can thank you further, besides accepting the answer? I had a bounty on this that unfortunately had already expired, so it feels a bit wrong that those points went to waste.Diddle
Ha! I dont know. I just started using stackoverflow. ;) but I'm here to help other people as well.Nickynico
C
-1

Sure,

Before go to action run it:

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

It's help confirm popup.

Counterattack answered 15/3, 2017 at 15:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.