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

Mat*_*hew 1 ruby performance automation watir watir-webdriver

我遇到的主要问题是从表中提取数据,但任何其他一般提示也将受到欢迎。我正在处理的表大约有 25 列和不同数量的行(从 5 到 50 之间)。

目前我正在抓取表格并将其转换为数组:

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
Run Code Online (Sandbox Code Playgroud)

table = b.table(:index, 5).to_a当表有20行时,该操作需要一分多钟。看起来将 20 X 25 表的单元格放入数组应该非常快。我需要对 80 多个表执行此操作,因此最终需要 1-2 小时才能运行。为什么需要这么长时间以及如何提高速度?

我尝试过迭代表行,而无需先转换为数组,但性能没有任何改进:

b.table(:index, 1).rows.each do |row|
    # ...
Run Code Online (Sandbox Code Playgroud)

使用 Windows 7 和 Ubuntu 的结果相同。我也尝试过使用 Firefox 而不是 Chrome,没有明显的差异。

jar*_*rib 6

如果您只是从大页​​面读取数据,一个快速的解决方法是使用 Nokogiri:

require 'nokogiri'
doc = Nokogiri::HTML.parse(b.table(:index, 1).html))
Run Code Online (Sandbox Code Playgroud)

我很想看到更多细节。如果您可以提供演示该问题的代码 + HTML 示例,请将其归档到问题跟踪器中。