Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding webelement with xpath starting with

Tags:

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.

like image 386
mjgirl Avatar asked Sep 06 '12 11:09

mjgirl


People also ask

How do you write starting with XPath?

Using XPath- starts-with method, we can write the Java code along with the dynamic XPath location as: findElement(By. xpath("//*[starts-with(@id,'lst')]"));

How do I find the web element using XPath?

Go to the First name tab and right click >> Inspect. On inspecting the web element, it will show an input tag and attributes like class and id. Use the id and these attributes to construct XPath which, in turn, will locate the first name field.

Does XPath always start with?

Types of X-path The key characteristic of XPath is that it begins with the single forward slash(/) ,which means you can select the element from the root node.

Which of the following is a syntax for an XPath to find an element?

To identify the element with xpath, the expression should be //tagname[@attribute='value']. To identify the element with xpath, the expression should be //tagname[@class='value']. There can be two types of xpath – relative and absolute.


1 Answers

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")]
like image 102
Justin Ko Avatar answered Oct 20 '22 02:10

Justin Ko