gpt4 book ai didi

capybara - 通过 capybara 和 poltergeist 设置时,PhantomJS 不运行 javascript

转载 作者:行者123 更新时间:2023-12-03 03:09:27 25 4
gpt4 key购买 nike

我设置 capybara 驱动程序的方式似乎导致 phantomjs 不执行 javascript。为了确认这可以在 capybara /poltergeist之外工作,我使用了原始的watir-webdriver和相同的phantomjs安装,并且它确实运行了javascript。

这是我使用poltergeist 创建 capybara 驱动程序的代码:

require 'capybara'
require 'capybara/poltergeist'

include Capybara::DSL

Capybara.register_driver :poltergeist do |app|
options = {
:js_errors => true,
:timeout => 120,
:debug => false,
:phantomjs_options => ['--load-images=no', '--disk-cache=false'],
:inspector => true,
}
Capybara::Poltergeist::Driver.new(app, options)
end

Capybara.default_driver = :poltergeist
Capybara.javascript_driver = :poltergeist
Capybara.app_host = 'https://google.com'

visit('/')
fill_in('q', with: 'green cheese')
sleep 2

p all('span').map{|s| s.text}

当我运行时,我会像这样退出:

["", "", "+You", "", "Search", "", "Images", "", "Maps", "", "Play", "", "YouTube", "", "News", "", "Gmail", "", "More", "More", "", "", "", "Sign in", "Sign in", "", "", "", "", "× A faster way to browse the web Install Google Chrome", "", "", "", "", "Advertising ProgramsBusiness Solutions+GoogleAbout Google © 2013 - Privacy & Terms"]

这清楚地表明搜索建议没有显示,这意味着 javascript 没有运行。我已使用屏幕截图确认了这一点。

现在,当我运行以下仅使用 water-webdriver 的代码时,它可以工作:

require 'watir-webdriver'

b = Watir::Browser.new :phantomjs

b.goto 'https://google.com'
b.text_field(:name, 'q').set "green cheese"
sleep 2
p b.spans.map{|s| s.text }
b.close

这给出了以下输出:

["", "", "+You", "", "Search", "", "Images", "", "Maps", "", "Play", "", "YouTube", "", "News", "", "Gmail", "", "More", "More", "", "", "", "Sign in", "Sign in", "", "", "", "", "×\nA faster way to browse the web\nInstall Google Chrome", "", "", "", "", "Advertising ProgramsBusiness Solutions+GoogleAbout Google\n© 2013 - Privacy & Terms", "green cheese", "green cheese strain", "green cheese weed", "green cheese enchiladas", "green cheesecake", "green cheese moon", "green cheese enchiladas recipe", "green cheese strain review", "green cheese media group", "green cheese penny", "", "", "", ""]

那里的所有绿色奶酪清楚地表明搜索建议正在显示,这意味着 javascript 必须正在运行。我再次使用屏幕截图确认了这一点。

任何人都可以帮助我了解如何配置 Capybara/poltergeist 使其正常工作吗?

最后一点:这与 Rails 无关。这是一个独立的测试套件,您可以在这里找到它的完整代码:https://github.com/rschultheis/rspec_capybara_starter

最佳答案

我通过将 phantomjs 从 1.9.8 升级到 2.1.1 解决了这个问题。有关执行此操作的说明,请引用此 http://phantomjs.org/download.html .

关于capybara - 通过 capybara 和 poltergeist 设置时,PhantomJS 不运行 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22493654/

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