Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find an element by partial href?

How can I locate that element with xpath? (class name is not unique).

<a class="my-action" href="/my/path/page.html"></a>

Tried: //a[@href='/my/path/page.html'] but did not succeed. What is wrong?

like image 654
membersound Avatar asked Dec 20 '13 20:12

membersound


2 Answers

You can use contains like

//a[contains(@href, 'path/page.html')]

Append . to your XPath if necessary, which means start from current node, not globally.

.//a[contains(@href, 'path/page.html')]

For Selenium, it'd be better to use CSS Selector in your case.

a[href*='path/page.html']

However, you need provide more info on why your XPath doesn't work. Post exception message please. Maybe the issue isn't about partial href in XPath or not. Please avoid XY problem.

like image 114
Yi Zeng Avatar answered Sep 30 '22 10:09

Yi Zeng


example = browser.find_element_by_class_name("my-action"[href*='my']")

like image 36
Nicholas Thompson Avatar answered Sep 30 '22 10:09

Nicholas Thompson