【问题标题】:Search table for a particular text and click on the second cell of the same row in selenium在表格中搜索特定文本并单击 selenium 中同一行的第二个单元格
【发布时间】:2017-04-19 10:43:42
【问题描述】:

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>

提前致谢。

【问题讨论】:

  • 欢迎,梅赫克。请阅读how to ask a good question 上的帮助,特别是展示您已经尝试过的内容并告诉我们出了什么问题。

标签: java selenium


【解决方案1】:

使用以下 xpath 根据您的状态“进行中”点击 id

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

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

【讨论】:

    【解决方案2】:
    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..)

    【讨论】:

      【解决方案3】:

      你可以使用下面的xpath:

      //th[text()='In Progress']/preceding-sibling::th[1]

      【讨论】:

      • 感谢您的回答。它没有用。它说无法找到元素
      • 你的inprogess标签是否包含在td中?如果是,请相应更改
      【解决方案4】:
          // 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;
              }
      
          }
      

      【讨论】:

      • 感谢您抽出时间回答我的问题。但我在以下行收到以下错误 java.lang.IndexOutOfBoundsException: Index: 20, Size: 6: if ("In Progress".equals(tblCells.get(applicaitonStatus).getText())) {
      • SO 上不允许仅代码答案。请花一点时间总结一下您的代码的作用以及它如何回答问题。
      • Mehek,唯一标识表。可能是页面有多个表,并且 tblHeaders 列表正在捕获所有标题。 tblHeaders 的大小只能是 6。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-08
      • 1970-01-01
      • 1970-01-01
      • 2011-08-23
      • 1970-01-01
      相关资源
      最近更新 更多