I am facing an issue with locator that I have been trying to figure out with no success, so any help will be much appreciated.
There is a list of items/rows on the page, and I want to click a button on a particular item, based on text of a label that is shown in the item (label text is unique). The locator looks like this:
//items-table-listing-row[.//div[text()="unique_label"]]//*[contains(text(), “Add to cart”)]
When I try out the locator in dev tools it works exactly as expected, a single item (button) is located based on the label text. However, in the actual test run, it appears that the locator selects the first item/row and thus clicks the button on first row, instead of one provided via label.
Any ideas what could be the reason for such behaviour? Did I make a mistake, or could it be a glitch or particularity of Playwright library?
So in your code //items-table-listing-row[.//div[text()=“unique_label”]]//*[contains(text(), “Add to cart”)]unique_label is replaced by someting like ${label_text}and this doesn’t work?
You could use Playwright Codegen to check , if your locator is correct.
What language/platform you using Playwright with? You could also debug by setting breakpoints or going into an interactive shell session to execute the code step by step to see what is happening. Aside from any code issues/glitches, sometimes automation hits timing issues that you don’t see when testing things manually/slowly.
With the breakpoint to then step through code, or shell session, you can attempt to execute the locator “find” outside of normal script/code execution and see whether it returns correct element or not.
@falcon030
”unique_label” is replaced with another string (actually by using .replace()”.
However, I don’t think this part is the issue. Text gets replaced and locator is created, which seems to work as expected when trying it out manually in developer tools.
Logs also suggest that locator is created correctly. Unless Playwright subsequently generates a different locator for the actual use.
@daluu
Next thing I should try is definitely using breakpoints. And I also thought whether it is possible that the item that was supposed to be clicked is loaded first. And after that, another item takes its place in the table, which causes the misclick. I will look into that, thanks for the tip.
Maybe you can try to wait for rows to be available/stable (count them, and wait until number is stable). Or even verify if your specific locator row is stable and doesn’t change prior to click.