How to click on a href
link in Nightwatch?
The href
text is identical to each other so I can't use XPath text.
I tried:
browser.useXpath().click("//a[href='./basket/removeItem.html?id=5de6710f75a5750f3d88495b')]");
but that doesn't work.
How to click on a href
link in Nightwatch?
The href
text is identical to each other so I can't use XPath text.
I tried:
browser.useXpath().click("//a[href='./basket/removeItem.html?id=5de6710f75a5750f3d88495b')]");
but that doesn't work.
At minimal, correct your XPath to add @
in front of the href
attribute and remove the spurious )
:
//a[@href='./basket/removeItem.html?id=5de6710f75a5750f3d88495b')]
^add ^delete
If id
variation is a problem, exclude it via starts-with()
:
//a[starts-with(@href, './basket/removeItem.html')]
If there are multiple such @href
attributes on the page, you can select one via indexing (as shown by @Christine, +1):
//a[starts-with(@href, './basket/removeItem.html')][1]
OP added a link to an image (ugh) of the HTML that shows that the targeted a
link is in the document by virtue of a ::before
CSS pseudo element. In such cases, the element doesn't actually exist in the DOM to be selected as usual.
You can use the index in XPath to pick either the first or second link.
For the first link:
//a[@id='tst_remove_from_basket'][1]
For the second link:
//a[@id='tst_remove_from_basket'][2]