membersound
membersound

Reputation: 86855

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?

Upvotes: 5

Views: 5942

Answers (2)

Nicholas Thompson
Nicholas Thompson

Reputation: 11

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

Upvotes: 1

Yi Zeng
Yi Zeng

Reputation: 32885

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.

Upvotes: 10

Related Questions