为什么 Selenium 测试这么慢?
我正在编写一个可以(合法)下载一堆图像的抓取工具,但我遇到了一个问题。在相关页面上,页面加载完成后,通过CSS查找元素需要很长时间。例如,我认为脚本在这里挂起大约 10 分钟:
@@wait.until do
find_element_by_css(css_selector)
end
@@driver.find_element(:css => css_selector).text
def find_element_by_css(css_string)
@@wait.until do
@@driver.find_element(:css => css_string)
end
end
其中 css_selector = "table:nth-child(6) tr:nth-child(2) .view-value"
或类似的内容那。现在,这个东西实际上会挂起 10-20 分钟而不做任何事情。如果我删除对 wait 的调用,脚本将引发超时异常。
知道如何解决这个问题吗?任何帮助将不胜感激。
I am writing a scraper that downloads (legally) a bunch of images and I`ve run into an issue. On the relevant pages, after a page is done loading, it just takes too long find elements by css. So for example I think the script hangs up here for like 10 minutes:
@@wait.until do
find_element_by_css(css_selector)
end
@@driver.find_element(:css => css_selector).text
def find_element_by_css(css_string)
@@wait.until do
@@driver.find_element(:css => css_string)
end
end
Where css_selector = "table:nth-child(6) tr:nth-child(2) .view-value"
or something like that. Now, this thing would hang literally for 10-20 minutes without doing anything. And if I remove the call to wait
, the script will throw a timeout exception.
Any idea on how to fix this? Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Selenium 在使用 css 来定位 IE 中的元素时存在一个错误。如果您改用其他浏览器(例如 Firefox),您应该会看到性能的巨大改进。
Selenium has a bug using css to locate elements in IE. If you switch to using a different browser (such as Firefox) you should see a vast improvement in performance.
我认为问题出在有缺陷的硒上。我改用 nokogiri,问题就消失了。此外,我的应用程序的设计存在一些错误/不一致。
I think the problem was buggy selenium. I switched to nokogiri and the problem disappeared. Also, there were some errors/inconsistencies with design of my application.