1

最大 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.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'
4

1 に答える 1

2

最善の策は、Nokogiri の助けを借りて行要素を見つけることです。Željko Filipin は、これについて良いブログ記事を書いています - http://zeljkofilipin.com/watir-nokogiri

例として、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) 

入力している他の行にも同じ概念を適用できます。

これは、少なくとも私が使用していたテストテーブルに役立ちました.

于 2013-01-02T22:29:13.720 に答える