我遇到一个问题,即单击事件未触发(有时会,有时不会),在 headless 状态下运行浏览器时总是会发生这种情况。
我将像这样使用.click
方法
find("#peoplePreview > tbody > tr:first-of-type > td > a").click
当在 Debug模式下运行 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
谢谢
最佳答案
我已经多次看到这种情况发生在我身上......我总是将其范围缩小到某个时间问题。我怀疑该元素存在且可点击,但未完全加载,因此单击它时不会触发它应该发生的事件...
保持 headless 测试一致的解决方法是添加 sleep 或尝试单击循环中的元素,直到实现“.click”事件的预期结果。
例如:
10.times do
sleep 0.5
element.click
break if some_other_element[:disabled].nil? #result if the initial click was successful
end
这是一个非常时髦的解决方案,但也是我发现的唯一可靠的解决方案......
关于javascript - phantom js 中的点击事件并不总是触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32396423/