Reputation: 147
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();
}
}
Upvotes: 1
Views: 3803
Reputation: 2334
You can click the correct cust id's select button as below
Code
// 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 column 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();
}
}
Upvotes: 1
Reputation: 193088
To invoke click()
on the respective button with text as Select with reference to any of the Cust Id, you can write a function which will accept the Cust Id as a reference and invoke click()
on the corresponding element with text as Select as follows:
Function:
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();
}
Now you can call the function with the desired Cust Id from anywhere within your script as follows:
clickSelect("12345")
//or
clickSelect("23456")
//or
clickSelect("34567")
Upvotes: 1
Reputation: 29362
replace this line:
IList<IWebElement> tableRow = tableElement.FindElements(By.TagName("tr"));
To :
IList<IWebElement> tableRow = driver.FindElements(By.CssSelector("table#cust-found-table tr td:first-child"));
and you can retrieve it using for each loop :
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:
A better way would be to have one separate method for this requirement.
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();
}
}
}
and you can call it like this :
searchAndClick("Spiderman")
Upvotes: 1
Reputation: 3927
In Java, you can try like below
//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;
}
}
Upvotes: 1