gpt4 book ai didi

ruby - phantomjs 通过 watir-webdriver,不处理 javascript 警报

转载 作者:数据小太阳 更新时间:2023-10-29 06:58:28 25 4
gpt4 key购买 nike

我正在尝试自动执行网页上的某些任务,其中一部分包括单击一个链接,该链接将显示一个 javascript 警报,您必须在其中按“确定”。麻烦的是,当它到达那个点时,它会失败并出现以下错误

/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>'

网上查了一下,好像是related to ghostdriver的问题也许 phantomjs 做不到,但这个概念对我来说有点奇怪(它被称为 phantom*js*,毕竟,警报是非常基本的 javascript),但也许我'我看错了。

用 chrome 运行脚本,它完美运行。

最佳答案

存在一个已知问题,即 PhantomJS 无法正确处理 JavaScript。一种解决方法是通过运行禁用警告框:

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

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

  1. 转到页面
  2. 运行上面的命令
  3. 点击“删除”(或触发警告框的任何内容)
  4. 它会自动为您点击“确定”。

关于ruby - phantomjs 通过 watir-webdriver,不处理 javascript 警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19125696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com