I am having trouble working my head out, as what is the best way to search a specific Text (CustId) on a web table & once I have found the search text then click on a Select-Button present at the end of the row. Now, I do have an ElementId for the table and progressive ElementId on each Select-Button Sample of the source code given below --
<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>
This potentially may be a duplicate post, but couldn't find a similar one. Any help is appreciate.
Go get started I have done as below but couldn't get this working --
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();
}
}
答案 0 :(得分:1)
替换此行:
IList<IWebElement> tableRow = tableElement.FindElements(By.TagName("tr"));
收件人:
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();
}
}
OR:
一种更好的方法是针对此需求使用一种单独的方法。
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")
答案 1 :(得分:1)
在Java中,您可以尝试以下操作
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011/04/25</td>
<td>$320,800</td>
</tr>
<tr>
<td>Garrett Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td>2011/07/25</td>
<td>$170,750</td>
</tr>
</tbody>
答案 2 :(得分:1)
要引用相应的 Cust Id (客户ID),在相应按钮上用文本 Select 调用click()
,您可以编写一个函数,该函数将接受客户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();
}
现在,您可以在脚本中的任意位置使用所需的客户ID 调用函数,如下所示:
clickSelect("12345")
//or
clickSelect("23456")
//or
clickSelect("34567")
答案 3 :(得分:1)
您可以按如下所示单击正确的客户ID的选择按钮
代码:
//expected Customer ID needs to be specified here
String expectedCustID = "12345";
IWebElement tableElement = driver.FindElement(By.Id("cust-found-table"));
IList<IWebElement> tableRow = tableElement.FindElements(By.TagName("tr"));
foreach (IWebElement row in tableRow)
{
//Cust Id will be placed in Second Coloum and hence xpath is defined as td[2]
var custId = row.FindElement(By.XPath(".//td[2]")).Text;
if (custId.Equals(expectedCustID))
{
row.FindElement(By.TagName("button")).Click();
}
}