单击Selenium中的<td>标签中的行查找文本

use*_*518 1 selenium-webdriver

我的HTML代码是:

enter code here:
<table>
  <tbody>
    <tr>
      <td class="item item-selected" id="gwt-uid-537" role="menuitem">Test Customer 1<br>#34</td>
    </tr>

    <tr>
      <td class="item" id="gwt-uid-538" role="menuitem">TEST CUSTOMER 2<br>#1,9874563210</td>
    </tr>

    <tr>
      <td class="item" id="gwt-uid-539" role="menuitem">test <br>test</td>
    </tr>
  </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

我想要做的只是在断行标记之前找到客户名称(例如:TEST CUSTOMER 2),然后单击该行.

我试过如下:

String ExpCustName = "TEST CUSTOMER 2";

Thread.sleep(1000);

WebElement FindCust = driver.findElement(By.xpath("//div[@class='customer']/table/tbody/tr/td/input[@class='gwt-SuggestBox']"));
FindCust.sendKeys("TES");

List<WebElement> CustList = driver.findElements(By.xpath("//div[@class='suggestPopupMiddleCenterInner suggestPopupContent']//table//tr"));

for(int i=0;i<CustList.size();i++){
    String ActCustName = CustList.get(i).getText();
    System.out.println(ActCustName);

    Thread.sleep(1000);
    if(ActCustName.contains(ExpCustName)){
        CustList.get(i).click();
    }
} 
Run Code Online (Sandbox Code Playgroud)

Alg*_*giz 6

// find the customer table
WebElement table = driver.findElement(By.xpath("//div[@class='customer']/table"));

// find the row
WebElement customer = table.findElement(By.xpath("//tr/td[contains(text(), 'TEST CUSTOMER 2')]"));

// click on the row
customer.click();
Run Code Online (Sandbox Code Playgroud)

您可能需要调整两个xpath表达式以匹配您的特定页面.