我在思考时遇到了困难,因为在网络表格上搜索特定文本(CustId)的最佳方式是什么?一旦找到搜索文本,然后单击出现在的选择按钮该行的末尾。现在,我确实有一个表格的 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 作为引用,并在相应元素的文本为 Select如下:
click()
调用该函数,如下所示:
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();
}
clickSelect("12345")
//or
clickSelect("23456")
//or
clickSelect("34567")
//to get all rows in table
List<WebElement> rows=driver.findElements(By.xpath("//table[@id='cust-found-table']/tbody/tr"));
for (int i = 0; i < rows.size(); i++) {
//check text in first cell of each row
if(rows.get(i).findElement(By.xpath("td[1]")).getText().equals("requiredCustIdHere")) {
//click on third cell of required row
rows.get(i).findElement(By.xpath("td/button")).click();
break;
}
}
致:
IList<IWebElement> tableRow = tableElement.FindElements(By.TagName("tr"));
您可以使用 for 每个循环来检索它:
IList<IWebElement> tableRow = driver.FindElements(By.CssSelector("table#cust-found-table tr td:first-child"));
更好的方法是针对此要求采用一种单独的方法。
foreach (IWebElement row in tableRow){
if (row.Text.Equals("Superman")){
driver.FindElement(By.Xpath("//td[text()='Superman']/following-sibling::td[contains(@class,'text-center')]/button")).Click();
}
}
你可以这样称呼它:
public void searchAndClick(string name) {
IList<IWebElement> tableRow = driver.FindElements(By.CssSelector("table#cust-found-table tr td:first-child"));
foreach (IWebElement row in tableRow){
if (row.Text.Equals(name)){
driver.FindElement(By.Xpath("//td[text()='"+name+"']/following-sibling::td[contains(@class,'text-center')]/button")).Click();
}
}
}
searchAndClick("Spiderman")