I have a quick Java table-question. I have a table, containing 4 columns, and X rows. The column's are as follows: Quote Number, Name, Date, and an Add button. I'm trying to, after looking for a specific name in the table, click on the add-button on the row the name was found.
Example:
Quote# Name Date Add-Button
100001 John Doe1 01/01/1980 [Add]
100002 John Doe2 01/01/1981 [Add]
100003 John Doe3 01/01/1982 [Add]
100004 John Doe4 01/01/1983 [Add]
I've written the following code, to identify if a certain name is in the table:
public List<String> getInternetQuoteNames()
{
List<WebElement> webName = sh.getElements(newInternetQuotesNames);
List<String> stringName = new ArrayList<>();
for(WebElement name : webName)
stringName.add(name.getText());
return stringName;
}
And am now trying to write another method that clicks on the correct [Add]-button, but I'm not sure how to access the correct row as I'm not good with tables. What I wrote so far is:
public void addInternetQuoteByName(String name)
{
List<String> listOfNames = getInternetQuoteNames();
if(listOfNames.contains(name))
{
// Find row of provided name
// Click on [Add]-button on that row
}
}
The HTML code for the Table is as follows:
<tbody>
<tr>
<td>100040365822</td>
<td>Test06232016 Test033929</td>
<td>06/23/2016</td>
<td>
<a id="assignQuoteLink_100040365822" class="add_from_pqs" href="#">Add Quote</a>
</td>
</tr>
<tr class="alt">
<td>100040365772</td>
<td>Test06232016 Test033723</td>
<td>06/23/2016</td>
<td>
<a id="assignQuoteLink_100040365772" class="add_from_pqs" href="#">Add Quote</a>
</td>
</tr>
<tr>
<tr class="alt">
<tr>
<tr class="alt">
</tbody>
Any tips on how to complete the method and click on the right row's [Add]-button is greatly appreciated!
Give a try to this approach:
driver.findElements(By.xpath("table xpath")).size()Code
public static void addInternetQuoteByName(String Name)
{
WebDriver driver = new FirefoxDriver();
int rowCount = driver.findElements(By.xpath("xpath of table")).size();
for (int i = 1; i <= rowCount; i++)
{
String sCellValue = driver.findElement(By.xpath("XPATH Of Name row")).getText();
if (sCellValue.equalsIgnoreCase(Name))
{
driver.findElement(By.xpath("xpath of add")).click();
}
}
driver.close();
}
xpath for name and add button have to passed as making the tr[i]dynamic
for eg: .//*[@id='content']/table/tbody/tr["+i+"]/td[2]
As I was not sure of exact xpath for name and add button column I have provided an example.
Let me know if you have issues.
Here's a function to does what you requested. It grabs all the TRs, looks in the 2nd cell (TD) for the name and if found, clicks the "Add Quote" link for that row.
public void addByName(String name)
{
List<WebElement> rows = driver.findElements(By.tagName("tr"));
for (WebElement row : rows)
{
List<WebElement> cells = row.findElements(By.tagName("td"));
if (cells.get(1).getText().equals(name))
{
cells.get(3).findElement(By.linkText("Add Quote")).click();
return;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With