Capybara / Poltergeist无法通过css选择器找到元素

时间:2016-08-26 22:39:11

标签: html ruby capybara poltergeist site-prism

当我使用selenium_webdriver并显示firefox时,工作正常;现在我迁移到无头浏览器,并在我的自动化测试中配置Poltergeist / PhantomJS,而不是工作。 Capybara / Poltergeist不再找到元素了。

我使用siteprism映射我的元素,这里是我的例子:element: link, "a[href='\my_link']"

当我的测试运行时,抛出此错误: Capybara::ElementNotFound: Unable to find css "a[href='/my_link']"

但当我使用方法find_all时,capybara / poltergeist找到我的元素:

2016-08-26T19:27:21 [DEBUG] WebPage - evaluateJavaScript "(function() { return (function() { return PoltergeistAgent.stringify((function (name, args) {\n      return __poltergeist.externalCall(name, args);\n    }).apply(this, PoltergeistAgent.JSON.parse(\"[\\\"find\\\",[\\\"css\\\",\\\"a[href='/my_link']\\\"]]\"))) })(); })()"
2016-08-26T19:27:21 [DEBUG] WebPage - evaluateJavaScript result QVariant(QString, "{\"value\":[557]}")
{"command_id":"fc012779-4b24-4659-afba-14180c3aa717","response":{"page_id":1,"ids":[557]}} 
=> []#<Capybara::Result:0x3fd82e69da98>

这是我对Poltergeist驱动程序的配置:

#env.rb

Capybara.default_max_wait_time = 10
    Capybara.register_driver :poltergeist do |app|
    options = {
        :js_errors => true,
        :timeout => 120,
        :debug => true,
        :window_size => [1280, 1024],
        :phantomjs_logger => File.open(FILE_PATH + "test_phantomjs.log", "w+"),
        :phantomjs_options => ['--load-images=no', '--disk-cache=false', '--ignore-ssl-errors=yes', '--debug=true', '--ssl-protocol=TLSv1'],
        :inspector => false,
    }
    Capybara::Poltergeist::Driver.new(app, options)
   end
    Capybara.javascript_driver = :poltergeist
    Capybara.default_driver = :poltergeist
    Capybara.current_driver = :poltergeist 

steps.rb

 Given(/^first step (\w+)$/) do |client_label|
  @client = @client[client_label] ||= get_client(@clients.values.map{|s| s.name})
  @app = App.new
  @app.home.load
end

And(/^step example (\w+)$/) do |var|
  @var = var
  @app.home.action(@var)
end

app.rb

class App
def home
    Home.new
end

def login
    Login.new
end

home.rb

class Home < SitePrism::Page
set_url "https://#{USERNAME}:#{PASSWORD}@#{URL}"

element :link_account,  "a[href='/account']"
element :link_1, "a[href='/link_1']"
element :link_2, "a[href='/link_2']"
element :link_3, "a[href='/link_3']"

##########################################
##########Methods of this page############
##########################################

def action(element)
    element = mapping_elements(element)
    test(element)
end

 def mapping_elements(var)
        case var
        when "option1"
            element = link_1
        when "option2"
            element = link_2
        when "option3"
            element = link_3
        end
    end
end

utils.rb

def test(element)
  click_element(element)
end

def click_element(element)
 element.click
end

我在我的Poltergeist注册驱动程序中使用了:inspector => true来打开chrome来查看我的页面;

我使用chrome console document.querySelectorAll("a[href='/link_3']")来选择此元素。

并返回正确的元素; [<a class=​"foobar" title=​"Title" href=​"/​link_3​">​text​</a>​]

如果有人帮助我,我真的很感激;

韩国社交协会。

1 个答案:

答案 0 :(得分:0)

旧答案,但值得更新

  • 默认情况下,SitePrism反映了水豚只查找可见元素的逻辑。可以使用SitePrism对每个元素(visible: false)或全局Capybara.ignore_hidden_elements = false进行配置

考虑到最初的问题,最终还是想找到Poltergeist / detro的替代品-现在有无头的chrome / firefox