Reputation: 823
I got this "edit" button that allows me to edit a specific row within a grid
That edit button has this:
<a class="action-menu-item" data-bind="attr: {target: $col.getTarget($action()), href: $action().href}, text: $action().label, click: $col.getActionHandler($action())" data-repeat-index="0" target="_self" href="https://randompageee/backend/company/index/edit/id/967/key/ee96a07876ee1fbef91d5d22dfjrrfkjaf9d40/">Edit</a>
It's XPATH is:
//*[@id="container"]/div/div[3]/table/tbody/tr[2]/td[4]/a
I used this in my code:
WebElement elementProd = driver.findElement(By.xpath("//*[@id=\"container\"]/div/div[2]/div[1]/div[5]/div/div/button[2]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();",elementProd);
The thing is it sometimes work but sometimes it doesn't.
Is anything wrong with the XPATH? Should i use other instead?
It has this:
<td data-bind="css: getFieldClass($row()), click: getFieldHandler($row()), template: getBody()" class="data-grid-actions-cell">
<!-- ko if: $col.isSingle($row()._rowIndex) --><!-- ko repeat: {foreach: $col.getVisibleActions($row()._rowIndex), item: '$action'} --><a class="action-menu-item" data-bind="attr: {target: $col.getTarget($action()), href: $action().href}, text: $action().label, click: $col.getActionHandler($action())" data-repeat-index="0" target="_self" href="randomdfsd/backend/company/index/edit/id/967/key/ee96a07876ee1fbef91d5d22507d5882e9f03b78dasb24657d3ce9d40/">Edit</a><!-- /ko --><!-- /ko -->
<!-- ko if: $col.isMultiple($row()._rowIndex) --><!-- /ko -->
</td>
It's XPATH is:
//*[@id="container"]/div/div[3]/table/tbody/tr[2]/td[4]
On the other hand i got this:
It contains:
<a class="action-menu-item" data-bind="attr: {target: $col.getTarget($action()), href: $action().href}, text: $action().label, click: $col.getActionHandler($action())" data-repeat-index="0" target="_self" href="https://randomdfsd/backend/company/index/edit/id/967/key/ee9ds1d5d22507d5882e9f03b78fca79514fab24657d3ce9d40/">Edit</a>
It's XPATH is:
//*[@id="container"]/div/div[3]/table/tbody/tr[2]/td[4]/a
Upvotes: -1
Views: 78
Reputation: 18980
The best approach for Selenium Webdriver to identify individual tr's (rows) or td (row/column combination) is to have the developer of the website (that you are testing) either place a unique id in those tags when the table is dynamically generated such as below (see reference #1). Then obtain the row or cell of the table using that id (see reference #2) rather than using XPath. If there are embedded tags under the tr or td, then it's safe to use XPath after that since the starting position of the HTML hierarchy of the element is fixed (see reference #3).
Reference #1:
<table><tr><td id="table1_row1_column1"><a>link</a></td></tr></table>
Reference #2:
WebElement elementTableTd = driver.findElement(By.id("table1_row1_column1"));
Reference #3:
WebElement elementTableA = driver.findElement(By.xpath("//*[@id=\"table1_row1_column1\"]/a"));
Upvotes: 1