Context So I have to make an automated test for my work. I come across this problem: - I have a table with 'subcontractors' - If I create an 'subcontractor' it will get in to the list (table) - But always on a random place (row 1, row 2, row 8 whatever) - Also the ID's change everytime. - All the rows have 4 buttons in the last (7th) column. - With the same names and the same ID as the tablerow.
Quesiton
I have to click on the button on lets say, the 4th row.
How can I do this?
If I use Xpath it will just click the button on the first row.
Thanks
EDIT! //get table WebElement baseTable = driver.findElement(By.id("gridSubContractors"));
String first_part = "//*[@id='gridSubContractors']/tbody/tr[";
String second_part = "]/td[3]";
String final_xpath;
int numOfRow = baseTable.findElements(By.tagName("tr")).size();
boolean found = false;
WebElement cellIneed = null;
for (int i=1; i<=numOfRow && found==false; i++){
final_xpath = first_part+i+second_part;
String test_name = baseTable.findElement(By.xpath(final_xpath)).getText();
System.out.println(test_name);
if (test_name.equals(subcon)){
WebElement tableRow = baseTable.findElement(By.xpath(final_xpath));
cellIneed = tableRow.findElement(By.xpath("//td[7]"));
found=true;
}
}
if (cellIneed != null){
cellIneed.findElement(By.xpath("//a [@data-original-title='Subcontractor name']")).click();
}
That is my code, cant realy get thw code in here. but what this code does is click the first button in the table with the name 'Subcontractor name' and not the one on the row that has been found with "if (test_name.equals(subcon)". subcon = the name of the subcontractor that needs to be found in the table. it is created by this: "subcon = name.getTextContent();" earlier in the code. hope this is information you need