2016-03-27 22 views
0

我經歷了這個網站,但找不到解決方案,並想知道是否有更多Selenium經驗的人可以幫我解決問題。 很長一段文章的道歉,但我想確保我解釋得很好。Selenium Webdriver:找到基於多個行值的複選框

我正在使用Java。我通常使用Rational Functional Tester(Java),但我試圖查看是否可以從某些測試中重寫代碼,以查看轉換的容易程度。我有一個11列和4行(但它可能是20或50行,它在測試過程中波動)表。 我可以讀取表格及其內容來查找我想要的行。我比較前10個單元格值中的每一個,如果它們全部匹配,我想單擊列11中的複選框。否則,繼續到下一行。

該函數正常工作以檢索每個單元格的值。但是,當我嘗試點擊複選框時,它總是選擇第一行中的一個。 當我檢查屬性時,我可以看到所有複選框具有相同的「名稱」,但具有不同的值(如1330361,1330363,1330359等)。 有趣的是,如果我搜索行中的所有複選框,則會報告其中的4個(在整個表中找到的數字,而不是在行中)。

我可能犯了一個非常基本的錯誤,但我無法弄清楚它是什麼。

我使用下面的代碼在表中進行搜索。該函數接收表格行,此時,我只是報告單元格值,然後嘗試單擊行中的複選框。這是一種調試功能。 我不確定發佈大量代碼是否合適,因此我只會放一個簡短版本的代碼,在這裏我嘗試點擊表格中的每個複選框。

// Passing a specific table row to the function: 
List<WebElement> columns=myRow.findElements(By.tagName("td")); 
for (int iLoop = 0;iLoop < columns.size();iLoop++) 
{ 
    System.out.println("Column " + iLoop + " : '" + columns.get(iLoop).getText().toString() + "'"); 
    // code to compare actual and expected values and setting of a boolean variable. 

    if (iLoop == 10 && recordMatch) 
    { 
      tCheckbox = myRow.findElement(By.xpath("//input[@type='checkbox']")); 
     System.out.println("Value is :" + tCheckbox.getAttribute("value").toString()); 
      tCheckbox.click(); 
    } 

回答

0

獲取複選框的XPath將整個頁面作爲範圍。我會在前面加一個圓點來獲得相對於元素的範圍:

// Passing a specific table row to the function: 
List<WebElement> columns=myRow.findElements(By.tagName("td")); 
for (int iLoop = 0;iLoop < columns.size();iLoop++) 
{ 
    System.out.println("Column " + iLoop + " : '" + columns.get(iLoop).getText().toString() + "'"); 
    // code to compare actual and expected values and setting of a boolean variable. 

    if (iLoop == 10 && recordMatch) 
    { 
    tCheckbox = myRow.findElement(By.xpath(".//input[@type='checkbox']")); 
    System.out.println("Value is :" + tCheckbox.getAttribute("value").toString()); 
    tCheckbox.click(); 
    } 
} 

但更好的方法是迭代列之前收集的所有複選框:如果你想

// Passing a specific table row to the function: 
List<WebElement> columns=myRow.findElements(By.xpath("//td[.//input[@type='checkbox']]")); 
List<WebElement> checkboxes=myRow.findElements(By.xpath("//td//input[@type='checkbox']")); 
for (int iLoop = 0;iLoop < columns.size();iLoop++) 
{ 
    System.out.println("Column " + iLoop + " : '" + columns.get(iLoop).getText().toString() + "'"); 
    // code to compare actual and expected values and setting of a boolean variable. 

    if (iLoop == 10 && recordMatch) 
    { 
    tCheckbox = checkboxes.get(iLoop); 
    System.out.println("Value is :" + tCheckbox.getAttribute("value").toString()); 
    tCheckbox.click(); 
    } 
} 
+0

弗洛朗嗨, 哇!這很好。我非常感謝你。 我想我真的需要在xpath知識上工作。出於某種原因,它只是沒有陷入。我很習慣與RFT合作。 Andre –

0

單擊基於值複選框,你必須列出使用for循環的所有值和複選框絕對XPath和通過「我」的價值,下面 見代碼:

 WebElement table = driver.findElement(By.xpath("//div[@id ='content']/table")); 
     /*List <WebElement> head = driver.findElements(By.tagName("th")); 
     head.size(); 

     for (int h=0;h<head.size();h++){*/ 
     List <WebElement> row = driver.findElements(By.tagName("tr")); 
     row.size(); 


     for (int i=0;i<row.size();i++){ 



      List <WebElement> col = row.get(i).findElements(By.tagName("td")); 
      col.size(); 


      for (int j=0;j<col.size();j++){ 
       String cv = col.get(j).getText(); 
// If name is Saudi Arabia, it will click on check box 
       if(cv.equalsIgnoreCase("Saudi Arabia")){ 
// Divide the x path where colunm is changing html/body/div[1]/div[5]/div[2]/div/div/table/tbody/tr[pass i value]/td[6]"; 
        String xp1 = "html/body/div[1]/div[5]/div[2]/div/div/table/tbody/tr["; 
        String xp2 = "]/td[6]"; 

// Pass the i value xp1+i+xp2 
        driver.findElement(By.xpath(xp1+i+xp2)).click(); 
       } 
       System.out.println(cv); 
      } 




     } 
相關問題