-1

I got this "edit" button that allows me to edit a specific row within a grid

enter image description here

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?


EDIT: enter image description here

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:

enter image description here

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
20
  • 1
    This other sister site is meant for questions like these, involving testing. sqa.stackexchange.com Commented Dec 15, 2022 at 18:49
  • The answer is likely not having a wait command. Selenium has a bug where it sometimes won't grab the element if a wait is not implemented. Search the other site, and you'll find it. Commented Dec 15, 2022 at 18:52
  • Hey @MacGyver thanks for commenting!. Is there any other way to name the XPATH? Commented Dec 15, 2022 at 18:54
  • 1
    precisely .. that will make this easier Commented Dec 15, 2022 at 19:22
  • 1
    @new_programmer yes, is a good idea to ask developers for adding ids for the elements you will use for your automation Commented Dec 15, 2022 at 19:22

1 Answer 1

1

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"));
Sign up to request clarification or add additional context in comments.

3 Comments

Hey Mac, thank you for replying!. What do you mean when you say: "If there are embedded tags under the tr or td". How?
You're welcome. "embedded" means to make an internal part of. So in my example, I have an "a" (HTML anchor tag/element) inside the "td" (HTML table data tag/element). So first I'm identifying the td WebDriver element using the custom id attribute of the td tag, and then adding the embedded "a" afterwards in reference 3.
meant to say "integral", not "internal".. merriam-webster.com/dictionary/embed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.