Ruby 使用Watir在HTML表中迭代和填充数据的更好方法
我有一个表,它可能包含多达50行和9列。然而,我用来填写表中数据的代码花费了很长时间 有没有更快的方法? 这是我的密码Ruby 使用Watir在HTML表中迭代和填充数据的更好方法,ruby,selenium,watir,watir-webdriver,performance,Ruby,Selenium,Watir,Watir Webdriver,Performance,我有一个表,它可能包含多达50行和9列。然而,我用来填写表中数据的代码花费了很长时间 有没有更快的方法? 这是我的密码 table = $browser.div(:id => "market").table(:id => 'tableTradeIndMarket') i = 3 + rand(1..table.rows.length-4) table.rows[i].cells[4].select_list.select 'Buy'
table = $browser.div(:id => "market").table(:id => 'tableTradeIndMarket')
i = 3 + rand(1..table.rows.length-4)
table.rows[i].cells[4].select_list.select 'Buy'
table.rows[i].cells[5].select_list.select 'Market'
table.rows[i].cells[6].text_field.set ($share)
table.rows[i+1].cells[4].select_list.select 'Buy'
table.rows[i+1].cells[5].select_list.select 'Limit'
table.rows[i+1].cells[6].text_field.set ($share)
//To take out dollar sign which is found on the second column and put that value in to another column of the same row
table.rows[i+1].cells[8].text_field.set(
table.rows[i+1].cells[2].text[1..table.rows[i+1].cells[2].text.length]
)
table.rows[i+1].cells[9].select_list.select 'Day'
table.rows[i+2].cells[4].select_list.select 'Buy'
table.rows[i+2].cells[5].select_list.select 'Stop'
table.rows[i+2].cells[6].text_field.set ($share)
table.rows[i+2].cells[7].text_field.set ( table.rows[i+2].cells[2].text[1..table.rows[i+2].cells[2].text.length])
table.rows[i+2].cells[9].select_list.select 'GTC'
table.rows[i+3].cells[4].select_list.select 'Buy'
table.rows[i+3].cells[5].select_list.select 'Stop/Limit'
table.rows[i+3].cells[6].text_field.set ($share)
table.rows[i+3].cells[7].text_field.set ( table.rows[i+3].cells[2].text[1..table.rows[i+3].cells[2].text.length])
table.rows[i+3].cells[8].text_field.set ( table.rows[i+3].cells[2].text[1..table.rows[i+3].cells[2].text.length])
table.rows[i+3].cells[9].select_list.select 'Day'
您的最佳选择可能是在Nokogiri的帮助下找到row元素。Željko Filipin在博客上发表了一篇关于这方面的好文章- 例如,第i行的输入为:
row_css = Nokogiri::HTML(browser.html).at_css("table#tableTradeIndMarket tr:nth-of-type(#{i})").css_path
row = browser.element(:css, row_css).to_subtype
row.cells[4].select_list.select 'Buy'
row.cells[5].select_list.select 'Market'
row.cells[6].text_field.set ($share)
您可以对正在输入的其他行应用相同的概念
这至少对我使用的测试表有帮助。您能提供HTML的源代码吗?