c# - 使用 selenium C# 在一行中单击操作链接(删除、查看、编辑)

标签 c# selenium xpath selenium-webdriver

我想寻求一些帮助,了解如何单击表格中的操作链接。

HTML 页面如下所示。

Lebron James     | EDIT | VIEW | DELETE
Chris Paul       | EDIT | VIEW | DELETE
Carmelo Anthony  | EDIT | VIEW | DELETE

html 格式如下所示。

<tr>
     <td>Lebron James</td>
     <td>
          <a href="/Player/Edit/282">Edit</a>
          <a href="/Player/View/282">View</a>
          <a href="/Player/Delete/282">Delete</a>
     </td>
</tr>

我已经尝试了下面的语法。

  1. webDriver.FindElement(By.XPath("//td[contains(text(), 'Chris Paul')]" + webDriver.FindElement(By.CssSelector("//td/a[包含(href=\"/Player/Delete/)]"))).Click();
  2. webDriver.FindElement(ByXPath("//td[contains(text(), 'Carmelo Anthony')]//td/a(href="/Player/Delete")")).Click( );

上面的语法可能与我之前尝试过的不准确,但它们看起来很相似。我能够在行中找到名称,但无法单击与名称平行的操作链接。

最佳答案

您应该尝试将 xpathfollowing-sibling axes 一起使用如下:-

  • 点击编辑链接:-

    webDriver.FindElement(By.XPath(".//td[text() = 'Lebron James']/following-sibling::td/a[text() = 'Edit']")).click();
    
  • 点击查看链接:-

    webDriver.FindElement(By.XPath(".//td[text() = 'Lebron James']/following-sibling::td/a[text() = 'View']")).click();
    
  • 点击删除链接:-

    webDriver.FindElement(By.XPath(".//td[text() = 'Lebron James']/following-sibling::td/a[text() = 'Delete']")).click();
    

关于c# - 使用 selenium C# 在一行中单击操作链接(删除、查看、编辑),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39624719/

相关文章:

html - 如何通过 XPath 选择第一个元素?

python - 如何在 Python 中使用 XPath?

c# - 将命令行参数传递给我的应用程序的已运行实例

javascript - 获取 Google Static Maps API 的西南 Angular 和东北 Angular

c# - 如何在 NSGridView 或 NSTableView 中跨列

c# - C#是否支持运算符覆盖

java - 从上次测试中恢复测试用例

java - 使用 PageFactory.initElements 在父类中初始化子类 Web 元素

java - 如何比较字符串列表中的元素与字符串数组列表?

xpath - 在带有 CXFRS 端点和 xPath 的 Camel 中遇到基于内容的路由问题