Selenium – Finding webelement with xpath starting with

selenium

I'm trying to locate a link using Selenium Webdriver. I don't wanna locate it by link text, only the actual link which is typed in . This can be done by using Selenium's find element by xpath – method, but what is the syntax when I only know the starting of that href text, not the whole text?

I guess I'm seaching for xpath link locator with some sort of starts with-method. I have no code ready on this.

Best Answer

You can use the start-with in xpath to locate an attribute value that starts with a certain text.

For example, assume you have the following link on the page:

<a href="mylink_somerandomstuff">link text</a>

Then you can use the following xpath to find links that have an href that starts with 'mylink':

//a[starts-with(@href, "mylink")]
Related Topic