使用 ruby​​ watir 测试 html 表格元素是否存在

发布于 2025-01-03 05:12:04 字数 2243 浏览 4 评论 0原文

我想测试的是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原谅过去的我 2025-01-10 05:12:04

有一个 #exists? 方法。

if browser.table.exists?
  # go on
end

There is an #exists? method.

if browser.table.exists?
  # go on
end
凹づ凸ル 2025-01-10 05:12:04

翻转你的逻辑。

if browser.text.include? ("Sorry. We couldn't find any properties for the postcode")
   puts "no table here"
   #quit/close/move on to next test/etc
else
   #do some stuff
end

您可以按照自己的方式进行操作,但是该收集方法非常脆弱 - 如果表格或上面的任何内容每次移动,脚本都会失败。

search_result = browser.table(:index, 0) #or :index, 1 for WATIR 1.X

if search_result.exists?
   #do some stuff
else
   puts "table not found"
end

Flip your logic.

if browser.text.include? ("Sorry. We couldn't find any properties for the postcode")
   puts "no table here"
   #quit/close/move on to next test/etc
else
   #do some stuff
end

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.

search_result = browser.table(:index, 0) #or :index, 1 for WATIR 1.X

if search_result.exists?
   #do some stuff
else
   puts "table not found"
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文