简体   繁体   English

如何提高watir-webdriver自动脚本的性能

[英]How can I increase the performance of watir-webdriver automated scripts

The main problem I'm having is pulling data from tables, but any other general tips would be welcome too. 我遇到的主要问题是从表中提取数据,但是也欢迎任何其他一般性提示。 The tables I'm dealing with have roughly 25 columns and varying numbers of rows (anywhere from 5-50). 我正在处理的表大约有25列和不同数量的行(从5到50)。

Currently I am grabbing the table and converting it to an array: 目前,我正在抓表并将其转换为数组:

require "watir-webdriver"
b = Watir::Browser.new :chrome
b.goto "http://someurl"

# The following operation takes way too long
table = b.table(:index, 1).to_a

# The rest is fast enough
table.each do |row|
    # Code for pulling data from about 15 of the columns goes here
    # ...
end
b.close

The operation table = b.table(:index, 5).to_a takes over a minute when the table has 20 rows. 当表有20行时,操作table = b.table(:index, 5).to_a将花费一分钟以上的时间。 It seems like it should be very fast to put the cells of a 20 X 25 table into an array. 将20 X 25表的单元格放入数组似乎应该很快。 I need to do this for over 80 tables, so it ends up taking 1-2 hours to run. 我需要对80多个表执行此操作,因此最终需要1-2个小时来运行。 Why is it taking so long and how can I improve the speed? 为什么要花这么长时间,如何提高速度?

I have tried iterating over the table rows without first converting to an array as well, but there was no improvement in performance: 我尝试遍历表行而不先转换为数组,但是性能没有改善:

b.table(:index, 1).rows.each do |row|
    # ...

Same results using Windows 7 and Ubuntu. 使用Windows 7和Ubuntu的结果相同。 I've also tried Firefox instead of Chrome without a noticeable difference. 我也尝试使用Firefox而不是Chrome,但没有明显的区别。

A quick workaround would be to use Nokogiri if you're just reading data from a big page: 如果您只是从一个大页面读取数据,一种快速的解决方法是使用Nokogiri:

require 'nokogiri'
doc = Nokogiri::HTML.parse(b.table(:index, 1).html))

I'd love to see more detail though. 我希望看到更多细节。 If you can provide a code + HTML example that demonstrates the issue, please file it in the issue tracker . 如果您可以提供演示该问题的代码+ HTML示例,请将其归档在问题跟踪器中

The #1 thing you can do to improve the performance of a script that uses watir is to reduce the number of remote calls into the browser. 可以提高使用watir的脚本性能的第一件事是减少浏览器的远程调用次数。 Each time you locate or operate on a DOM element, that's a call into the browser and can take 5ms or more. 每次定位或操作DOM元素时,这都是对浏览器的调用,可能需要5毫秒或更长时间。

In your case, you can reduce the number of remote calls by doing the work on the browser side via execute_script() and checking the result on the ruby side. 在您的情况下,您可以通过execute_script()在浏览器端进行工作并在ruby端检查结果来减少远程调用的次数。

When attempting to improve the speed of your code it's vital to have some means of testing execution times (eg ruby benchmark ). 当试图提高代码速度时,至关重要的是要有一些测试执行时间的方法(例如ruby基准 )。 You might also like to look at ruby-prof to get a detailled breakdown of the time spent in each method. 您可能还希望查看ruby-prof ,以详细了解每种方法所花费的时间。

I would start by trying to establish if it's not the to_a method rather than the table that's causing the delays on that line of code. 我将首先尝试确定不是to_a方法,而不是导致该行代码延迟的table Watir's internals (or nokogiri as per jarib's answer) may be quicker. Watir的内部结构(或按照jarib的回答为nokogiri)可能更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM