【问题标题】:Click Events in phantom js do not always fire幻影 js 中的点击事件并不总是触发
【发布时间】:2015-09-07 19:02:10
【问题描述】:

我遇到了一个点击事件没有触发的问题(有时会,有时不会),这总是在以无头状态运行浏览器时发生。

我会像这样使用.click 方法

find("#peoplePreview > tbody > tr:first-of-type > td > a").click

在调试模式下运行 phantomjs 时,我得到了

"name":"find","args":["css","#peoplePreview > tbody > tr:first-of-type > td > a"]}
{"response":{"page_id":18,"ids":[10]}}
{"name":"click","args":[18,10]}
{"response":{"position":{"x":114,"y":523.5}}}

据我所知,该元素已被找到,并且该元素在正确的坐标处单击,但未触发该事件

有人遇到过这种情况或有其他方法可以触发此事件吗?

这就是我设置 poltergeist 的方式

Capybara.register_driver :headless do |app|
  options = {
  :js_errors => false,
  :inspector => true,
  :debug => true,
  :path => '/usr/bin/',
  :phantomjs_options => ['--ignore-ssl-errors=yes', '--ssl-protocol=any']
 }
 Capybara::Poltergeist::Driver.new(app, options)
end

谢谢

【问题讨论】:

  • 请发布完整的代码,(通常你应该像这样调用点击方法...td > a").click();
  • 可能是 PhantomJS 中带有 :first-of-type 选择器的错误。尝试使用 XPath 而不是 CSS 选择器,并尽量不要专门为“tbody”编写。

标签: javascript phantomjs capybara


【解决方案1】:

我已经多次看到这种情况发生在我身上......我总是将其缩小到一些时间问题。我的怀疑是该元素存在且可单击但未完全加载,因此单击它时不会触发它应该触发的事件...

保持我的无头测试一致的解决方法是添加睡眠或尝试在循环中单击一个元素,直到实现“.click”事件的预期结果。

例如:

10.times do
  sleep 0.5
  element.click
  break if some_other_element[:disabled].nil? #result if the initial click was successful
end

这是一个非常时髦的解决方案,但却是我找到的唯一可靠的解决方案......

【讨论】:

    猜你喜欢
    • 2014-12-28
    • 1970-01-01
    • 2014-05-29
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-27
    相关资源
    最近更新 更多