gpt4 book ai didi

c# - 如何在 C# 中使用 Selenium 定位表格中的文本并引用元素单击另一个元素?

转载 作者:太空宇宙 更新时间:2023-11-03 20:54:56 29 4
gpt4 key购买 nike

我无法解决问题,因为在网络表上搜索特定文本 (CustId) 的最佳方法是什么?找到搜索文本后,单击 < em>选择按钮 出现在行的末尾。现在,我确实有一个表的 ElementId 和每个 Select-Button 上的渐进式 ElementId下面给出的源代码示例 --

<table class="w-table w-table-zebra w-table-hover" id="cust-found-table">
<tbody><tr>
<th class="w-table-header w-table-width-60">Cust name</th>
<th class="w-table-header w-table-width-25">Cust Id</th>
<th class="w-table-header w-table-width-15"></th>
</tr>
<!----><tr>
<td class="w-table-row">Superman</td>
<td class="w-table-row">12345</td>
<td class="w-table-row text-center">
<button class="btn btn-sm w-btn-jetson px-4" type="button" id="cust-found-btn-0">Select</button>
</td>
</tr><tr>
<td class="w-table-row">Spiderman</td>
<td class="w-table-row">23456</td>
<td class="w-table-row text-center">
<button class="btn btn-sm w-btn-jetson px-4" type="button" id="cust-found-btn-1">Select</button>
</td>
</tr><tr>
<td class="w-table-row">Batman</td>
<td class="w-table-row">34567</td>
<td class="w-table-row text-center">
<button class="btn btn-sm w-btn-jetson px-4" type="button" id="cust-found-btn-2">Select</button>
</td>
</tr><tr>

这可能是重复的帖子,但找不到类似的帖子。任何帮助表示赞赏。

开始吧,我已经完成了如下操作,但无法正常工作 --

var theSelector = "button[id*='" + cust-found-btn + "']";    
IWebElement tableElement = driver.FindElement(By.Id("cust-found-table"));
IList<IWebElement> tableRow = tableElement.FindElements(By.TagName("tr"));
IList<IWebElement> rowTD;
foreach (IWebElement row in tableRow)
{
rowTD = row.FindElements(By.TagName("td"));

if (row.Text.Equals("searchtext"))
{
driver.FindElement(By.Id("cust-found-btn")).Click();
}
}

最佳答案

要在相应按钮上调用 click(),并引用任何 Cust Id 将文本作为 Select,您可以编写一个该函数将接受 Cust Id 作为引用,并在相应元素上调用 click(),文本为 Select,如下所示:

  • 函数:

    public void clickSelect(string CustID)
    {
    driver.FindElement(By.XPath("//table[@class='w-table w-table-zebra w-table-hover' and @id='cust-found-table']//tr//td[@class='w-table-row'][text()='" + CustID + "']//following::td[1]/button[@class='btn btn-sm w-btn-jetson px-4' and starts-with(@id,'cust-found-btn-')]")).Click();
    }
  • 现在您可以从脚本中的任何位置调用具有所需Cust Id 的函数,如下所示:

    clickSelect("12345")
    //or
    clickSelect("23456")
    //or
    clickSelect("34567")

关于c# - 如何在 C# 中使用 Selenium 定位表格中的文本并引用元素单击另一个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51511343/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com