搜索特定文本的表格,然后单击selenium中同一行的第二个单元格

时间:2017-04-19 10:43:42

标签: java selenium

click to see Table image

你好,伙计们, 所以基本上我需要搜索正在进行的应用程序并单击应用程序ID。以下是HTML。

Html: emphasized text
<table cellspacing="0" cellpadding="0" border="0" class="list">
  <tbody>
    <tr class="headerRow">
      <th scope="col" class="actionColumn">Action</th>
      <th class="zen-deemphasize" scope="col">Application ID</th>
      <th class="zen-deemphasize" scope="col">Application Status</th>
      <th class=" zen-deemphasize" scope="col">Co-Applicant</th>
      <th class="DateElement zen-deemphasize" scope="col">Created Date</th>
      <th class=" zen-deemphasize" scope="col">Created By</th>
      <th class="DateElement zen-deemphasize" scope="col">Last Modified Date</th>
      <th class=" zen-deemphasize" scope="col">Last Modified By</th>
    </tr>

    <!-- ListRow -->
    <tr onmouseover="if (window.hiOn){hiOn(this);}" onmouseout="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}" onblur="if (window.hiOff){hiOff(this);}" class="dataRow even first">
      <td class="actionColumn"><a title="Edit - Record 1 - 27394022" class="actionLink" href="/a08Q00000071tCE/e?retURL=%2F001Q0000012jqbt">Edit</a>&nbsp;|&nbsp;<a title="Delete - Record 1 - 27394022" onclick="return confirmDelete();" class="actionLink" href="/setup/own/deleteredirect.jsp?delID=a08Q00000071tCE&amp;retURL=%2F001Q0000012jqbt&amp;_CONFIRMATIONTOKEN=VmpFPSxNakF4Tnkwd05DMHlNbFF4TURveE1Ub3pOeTR4TmpWYSxSTHpOd1ZjSC1CUmZfc3puYllINktsLFltVXpPVFUy">Del</a></td>
      <th
        class=" dataCell  " scope="row"><a href="/a08Q00000071tCE">27394022</a></th>
        <td class=" dataCell  ">In Progress</td>
        <td class=" dataCell  ">&nbsp;</td>
        <td class=" dataCell  DateElement">17/04/2017</td>
        <td class=" dataCell  "><a href="/005E0000005pAqf">Web API</a>, 17/04/2017 5:54 AM</td>
        <td class=" dataCell  DateElement">17/04/2017</td>
        <td class=" dataCell  "><a href="/005E0000005pAqf">Web API</a>, 17/04/2017 6:17 AM</td>
    </tr>

提前致谢。

4 个答案:

答案 0 :(得分:0)

List<WebElement> elements=driver.findElements(By.cssSelector("the css selector for all the elements in the table"));
for(WebElement e : elements){
    if(e.getText().contains("In progress")){               
        WebElement elmt = e.findElement(By.cssSelector("the css selector for the id"));
        elmt.click();
    }
}

您可以使用cssSelector或其他选择器(id / xpath ..)

答案 1 :(得分:0)

您可以使用以下xpath:

// th [text()=&#39; In Progress&#39;] / previous-sibling :: th [1]

答案 2 :(得分:0)

使用以下xpath根据状态&#39;进行中&#39;点击ID。你有

driver.findElement(By.xpath(".//table[@class='list']//td[normalize-space()='In Progress']/preceding-sibling::th/a")).click();

注意:如果您有多个相同的元素,请使用List来获取并迭代所有

答案 3 :(得分:-1)

    // Get table headers first
    List<WebElement> tblHeaders = driver.findElements(By.xpath(".//table/tbody/tr[@class='headerRow']/th"));
    int applicationId = 0;
    int applicaitonStatus = 0;
    // Get column numbers for Application Id, Application Status columns
    for (int colCount = 0; colCount < tblHeaders.size(); colCount++) {
        if ("Application Status".equals(tblHeaders.get(colCount).getText())) {
            applicaitonStatus = colCount;
        } else if ("Application Id".equals(tblHeaders.get(colCount)
                .getText())) {
            applicationId = colCount;
        }
    }

    // Get table rows
    List<WebElement> tblRows = driver.findElements(By
            .xpath(".//table/tbody/tr[contains(@class,'dataRow')]"));

    for (WebElement webElement : tblRows) {
        List<WebElement> tblCells = webElement.findElements(By
                .tagName("td"));
        // If Application status is In Progress
        if ("In Progress".equals(tblCells.get(applicaitonStatus).getText())) {
            //Click respective Application Id
            tblCells.get(applicationId).click();
            break;
        }

    }