2013-12-11 26 views
5

我想掃描HTML表中的行使用部分href xpath並執行進一步的測試與該行的其他列值。如何獲得一個HTML表格行與水豚

<div id = "blah"> 
    <table> 
    <tr> 
     <td><a href="afile?key=HONDA">link</a></td> 
     <td>29 33 485</td> 
     <td>45.2934,00 EUR</td> 
    </tr> 
    <tr> 
     <td><a href="afile?key=HONDA">link</a></td> 
     <td>22 93 485</td> 
     <td>38.336.934,123 EUR</td> 
    </tr> 
    <tr> 
     <td><a href="afile?key=something_else">link</a></td> 
     <td>394 27 3844</td> 
     <td>3.485,2839 EUR</td> 
    </tr>  
    </table> 
    </div> 

在黃瓜JVM的步驟定義,我進行了這麼多很容易像下面(我更舒適的使用Ruby)

@Given("^if there are...$") 
public void if_there_are...() throws Throwable { 
      ... 
      ... 
      baseTable = driver.findElement(By.id("blah")); 
      tblRows = baseTable.findElements(By.tagName("tr")); 

     for(WebElement row : tblRows) {             
      if (row.findElements(By.xpath(".//a[contains(@href,'key=HONDA')]")).size() > 0) { 
       List<WebElement> col = row.findElements(By.tagName("td")); 
       tblData dummyThing = new tblData(); 
       dummyThing.col1 = col.get(0).getText(); 
       dummyThing.col2 = col.get(1).getText(); 
       dummyThing.col3 = col.get(2).getText(); 
       dummyThing.col4 = col.get(3).getText(); 
       dummyThings.add(dummyThing); 
      } 
     } 

我無言以對這裏

page.find('#blah').all('tr').each { |row| 
    # if row matches xpath then grab that complete row 
    # so that other column values can be verified 
    # I am clueless from here 
    row.find('td').each do { |c| 

    } 
    page.find('#blah').all('tr').find(:xpath, ".//a[contains(@href,'key=HONDA')]").each { |r| 
    #we got the row that matches xpath, let us do something 
    } 
} 

回答

11

我想你正在做的事:

page.all('#blah tr').each do |tr| 
    next unless tr.has_selector?('a[href*="HONDA"]') 

    # Do stuff with trs that meet the href requirement 
    puts tr.text 
end 
#=> link 29 33 485 45.2934,00 EUR 
#=> link 22 93 485 38.336.934,123 EUR 

這基本上說來:

  1. 通過每個TRS的查找ID爲「嗒嗒」
  2. 迭代元素都TRS
  3. 如果TR不具有具有包含HONDA A HREF鏈接,請忽略它
  4. 否則,輸出該行的文本(與條件匹配)。你可以在這裏做任何你需要的東西。

您也可以使用xpath將上述內容合併到一個語句中。不過,我不認爲這是爲可讀:

page.all(:xpath, '//div[@id="blah"]//tr[.//a[contains(@href, "HONDA")]]').each do |tr| 
    # Do stuff with trs that meet the href requirement 
    puts tr.text 
end 
#=> link 29 33 485 45.2934,00 EUR 
#=> link 22 93 485 38.336.934,123 EUR 

下面是如何檢查每個匹配行的鏈接URL和列值的示例:

page.all('#blah tr').each do |tr| 
    next unless tr.has_selector?('a[href*="HONDA"]') 

    # Do stuff with trs that meet the href requirement 
    href = tr.find('a')['href'] 
    column_value_1 = tr.all('td')[1].text 
    column_value_2 = tr.all('td')[2].text 

    puts href, column_value_1, column_value_2 
end 
#=> file:///C:/Scripts/Misc/Programming/Capybara/afile?key=HONDA 
#=> 29 33 485 
#=> 45.2934,00 EUR 
#=> file:///C:/Scripts/Misc/Programming/Capybara/afile?key=HONDA 
#=> 22 93 485 
#=> 38.336.934,123 EUR 
+0

柯嗨,我如何才能在href與xpath匹配的鏈接。我需要用匹配行的列值檢查幾件事情。那麼,我如何獲得匹配的列(值)? – Bala

+0

您可以使用正常的發現者來檢查tr。已經添加了一個例子。 –

+0

我得到Selenium錯誤'在緩存中找不到元素 - 可能因爲查找過了頁面而發生了變化...'。但是,如果我簡單地'把tr.text'移除'tr.has_selector? – Bala