【问题标题】:Select a link in a table row based on cell content根据单元格内容选择表格行中的链接
【发布时间】:2017-06-29 15:37:21
【问题描述】:

使用 Selenium IDE,我试图在表格行中找到一个链接。表行是动态生成的。每行都有一些包含文本内容的单元格和一个可能包含两个或更多链接的单元格。

我想做的是创建一个自动化测试,点击特定文本引用的特定行中的特定链接。例如,在下表中:

<html>
<body>
<table border="1">
 <tr>
   <td>
    Hello World!
   </td>
   <td>
    Type Greeting
   </td>
   <td>
    <a href="www.foo.com/view/id=123456">View</a>
    <a href="www.foo.com/delete/id=123456">Delete</a>
   </td>
 </tr>
 <tr>
   <td>
    Buenos Dias!
   </td>
   <td>
    Type Greeting 
   </td>
   <td>
    <a href="www.foo.com/view/id=789101">View</a>
    <a href="www.foo.com/delete/id=789101">Delete</a>
   </td>
 </tr>
</table>
</body>
</html>

场景是,在前一页我添加了数据“Buenos Dias!”。下一页将显示所有添加数据的表格。我希望能够点击刚刚添加的数据的“查看”链接(在本例中为“Buenos Dias!”条目)。

通过引用文本“Buenos Dias!”找到“查看”链接的好方法是什么?

我能够找到包含文本的单元格:

//td[text()="Buenos Dias!"] 

但我想以某种方式使用它作为从表中定位其各自“视图”链接的参考。我尝试了兄弟格式(以下,前面),但我似乎无法让它正常工作。使用绝对 xpath (html/body/table/tbody/tr[1]/td[3]/a[1]) 将不起作用,因为数据可能会更改并且条目出现在不同的行上。

提前致谢,如果您需要更多信息,请告诉我。

  • Selenium IDE 1.7.2
  • 火狐 9.0.1
  • Mac OS X Lion 10.7.3

【问题讨论】:

    标签: xpath selenium-ide


    【解决方案1】:

    使用

    (//td[normalize-space() ="Buenos Dias!"])[1]/following-sibling::td[2]/a[. = 'View']
    

    【讨论】:

      【解决方案2】:
      //find table first
       WebElement q=  d.findElement(By.xpath("//*[@id='applicationcontent']/div[6]/table/tbody/tr"));
      
      // find a unique value of the row and click on link of the row which is after the text you find.
               q.findElement(By.xpath("//td[contains(text(),'abcd@gmail.com')]/following-sibling::td/a[@id='edit.png']")).click();
      

      【讨论】:

        【解决方案3】:

        (//td[normalize-space() ="Buenos Dias!"])[1]/following-sibling::td[2]/a[. = '查看']

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-05-04
          • 1970-01-01
          • 1970-01-01
          • 2021-03-11
          • 1970-01-01
          • 2020-05-16
          • 1970-01-01
          相关资源
          最近更新 更多