2014-12-04 71 views
0

Im試圖實現對錶中元素的點擊。此刻即時搜索表中的特定字符串。如果字符串匹配,我將元素返回給它的調用方法,並嘗試實現點擊,但調用方法從不單擊該元素。Selenium does not implement點擊

任何幫助,將不勝感激。

檢查表的方法。

public static WebElement chk_TableContentsByXpath(String searchString, String elements){ 
    WebElement element = null; 
     try{ 
      // Grab the table 
      WebElement table = driver.findElement(By.xpath(elements)); 

      // Now get all the TR elements from the table 
      List<WebElement> allRows = table.findElements(By.tagName("tr")); 
      // And iterate over them, getting the cells 
      for (WebElement row : allRows) { 
       List<WebElement> cells = row.findElements(By.xpath("./*")); 
      for (WebElement cell : cells) { 
       // System.out.println(cell.getText()); 
        if(cell.getText().equals(searchString)){ 
         element = cell; 
         return element; 
        } 
      } 
      } 
    }catch (Exception e){ 
       Log.error("Class Utils | Method GetTableContents | Exception occured while search table : "+e.getMessage()); 
       throw (e); 
      } 
    return element; 
     } 

調用方法

package appModules; 

import pageObjects.MC_Page_links; 
import pageObjects.MC_ProductTypes_Page; 
import pageObjects.TopNav_links; 
import utility.Constant; 
import utility.Utils; 

public class MC_MaterialProductType_UpdateProductType_Action { 

    public static void Execute(int iTestCaseRow) throws Exception{ 

     Utils.waitForElement(TopNav_links.lnk_MasterControl()); 

     TopNav_links.lnk_MasterControl().click(); 

     MC_Page_links.lnk_ProductTypes(); 

     Utils.chk_TableContentsByXpath(Constant.MC_ProductTypeName,Constant.MC_ProductTypesTable).click();//this line doesnt implement the click 

     MC_ProductTypes_Page.inpt_UpdateProductName().sendKeys(Constant.MC_ProductTypeNameUpdate); 

    } 
} 

回答

0

如果你正在尋找一個特定的字符串,您可以使用XPath:

WebElement table = driver.findElement(By.xpath(elements)); 

table.findElement(By.xpath("//tr[text() = '" + searchString + "']")).click(); 
0

您可以嘗試以下任一過程:

1- Tr ÿ替代代碼:

List<WebElement> cells = row.findElements(By.xpath("./*"));

List<WebElement> cells = row.findElements(By.xpath("//td"));

2-另外,還可以使用下面的代碼直接返回的元件,從該方法chk_TableContentsByXpath

WebElement table = driver.findElement(By.xpath(elements)); 

element = table.findElement(By.xpath("//td[contains(text(),searchString)]"));