使用 ruby watir 测试 html 表格元素是否存在
我想测试的是table元素是否存在?如果没有表,那么我只想结束脚本。但是,如果有一个表格,我想将其输出到 Excel。
该脚本正在测试两个网址:
http://www.mycounciltax.org。 uk/results?postcode=EX99AE&search=搜索
http://www.mycounciltax.org.uk/results?postcode=CV56bz&search =Search
第一个 url 呈现一个没有 html 表的网页,第二个呈现一个包含 html 表元素的网页。
我尝试将以下脚本放在一起,但我认为这是不正确的。我确信我在测试表元素时犯了一个错误。
if browser.table.trs.collect {|tr| [tr[0].text, tr[1].text, tr[2].text]}.exists?
then content = browser.table.trs.collect {|tr| [tr[0].text, tr[1].text, tr[2].text]}
如果您从下面的脚本中删除上面的代码,它将运行,但当它找不到 html 表时就会失败。
require "watir-webdriver"
browser = Watir::Browser.new :ff
browser.goto "http://www.mycounciltax.org.uk/results?postcode=CV56BZ&search=Search"
if browser.table.trs.collect {|tr| [tr[0].text, tr[1].text, tr[2].text]}.exists?
then content = browser.table.trs.collect {|tr| [tr[0].text, tr[1].text, tr[2].text]}
require 'win32ole'
application = WIN32OLE.new('Excel.Application')
application.visible = TRUE
workbook = application.Workbooks.Add();
worksheet = workbook.Worksheets(1);
worksheet.visible
row = 1; column = 0
content.each do |array|
array.each do |element|
worksheet.Cells(1,1).offset(row,column).value = element #.offset(row,column)
column += 1
end
row += 1
column = 0
end
else end
browser.goto "http://www.mycounciltax.org.uk/results?postcode=EX99AE&search=Search"
if browser.table.trs.collect {|tr| [tr[0].text, tr[1].text, tr[2].text]}.exists?
then content = browser.table.trs.collect {|tr| [tr[0].text, tr[1].text, tr[2].text]}
require 'win32ole'
application = WIN32OLE.new('Excel.Application')
application.visible = TRUE
workbook = application.Workbooks.Add();
worksheet = workbook.Worksheets(1);
worksheet.visible
row = 1; column = 0
content.each do |array|
array.each do |element|
worksheet.Cells(1,1).offset(row,column).value = element #.offset(row,column)
column += 1
end
row += 1
column = 0
end
else end
我打算立即运行上面的代码。谁能指出我哪里出错了?我是 ruby 和 watir 的新手:-)。
非常感谢。
I want to test is whether the table element exists? And if there is no table then I just want the script to end. However if there is a table I want to output it to excel.
The script is testing two url's:
http://www.mycounciltax.org.uk/results?postcode=EX99AE&search=Search
http://www.mycounciltax.org.uk/results?postcode=CV56bz&search=Search
The first url presents a web page without a html table and the second presents a web page which includes a html table element.
I have tried putting the following script together but I don't think this is correct. I am sure I have made a mistake with testing the for the table element.
if browser.table.trs.collect {|tr| [tr[0].text, tr[1].text, tr[2].text]}.exists?
then content = browser.table.trs.collect {|tr| [tr[0].text, tr[1].text, tr[2].text]}
If you remove the above code from the below script it will run but will fall over when it doesn't find the html table.
require "watir-webdriver"
browser = Watir::Browser.new :ff
browser.goto "http://www.mycounciltax.org.uk/results?postcode=CV56BZ&search=Search"
if browser.table.trs.collect {|tr| [tr[0].text, tr[1].text, tr[2].text]}.exists?
then content = browser.table.trs.collect {|tr| [tr[0].text, tr[1].text, tr[2].text]}
require 'win32ole'
application = WIN32OLE.new('Excel.Application')
application.visible = TRUE
workbook = application.Workbooks.Add();
worksheet = workbook.Worksheets(1);
worksheet.visible
row = 1; column = 0
content.each do |array|
array.each do |element|
worksheet.Cells(1,1).offset(row,column).value = element #.offset(row,column)
column += 1
end
row += 1
column = 0
end
else end
browser.goto "http://www.mycounciltax.org.uk/results?postcode=EX99AE&search=Search"
if browser.table.trs.collect {|tr| [tr[0].text, tr[1].text, tr[2].text]}.exists?
then content = browser.table.trs.collect {|tr| [tr[0].text, tr[1].text, tr[2].text]}
require 'win32ole'
application = WIN32OLE.new('Excel.Application')
application.visible = TRUE
workbook = application.Workbooks.Add();
worksheet = workbook.Worksheets(1);
worksheet.visible
row = 1; column = 0
content.each do |array|
array.each do |element|
worksheet.Cells(1,1).offset(row,column).value = element #.offset(row,column)
column += 1
end
row += 1
column = 0
end
else end
I plan to run the above code at once. Can anyone point out where I am going wrong? I am new to ruby and watir :-).
Many thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个
#exists?
方法。There is an
#exists?
method.翻转你的逻辑。
您可以按照自己的方式进行操作,但是该收集方法非常脆弱 - 如果表格或上面的任何内容每次移动,脚本都会失败。
Flip your logic.
You can do it your way, but that collect method is a very fragile - if the table or anything above it every moves, the script will fail.