Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath Find full HTML element ID from partial ID

Tags:

xpath

I am looking to write an XPath query to return the full element ID from a partial ID that I have constructed. Does anyone know how I could do this? From the following HTML (I have cut this down to remove work specific content) I am looking to extract f41_txtResponse from putting f41_txt into my query.

<input id="f41_txtResponse" class="GTTextField BGLQSTextField2 txtResponse"  value="asdasdadfgasdfg" name="f41_txtResponse" title="" tabindex="21"/>

Cheers

like image 227
user228178 Avatar asked Dec 15 '09 13:12

user228178


People also ask

Can we find element by partial ID match in Selenium?

We can locate elements by partial id match with Selenium webdriver. This can be achieved while we identify elements with the help of xpath or css locator. With the css and xpath expression, we use the regular expression to match id partially.

Which of the below methods can be used to match the ID attribute partially?

We can identify elements by partially comparing to its attributes in Selenium with the help of regular expression. In xpath, there is contains () method. It supports partial matching with the value of the attributes. This method comes as useful while dealing with elements having dynamic values in their attributes.

How do I find the XPath of an element in HTML?

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.

How can we locate elements using their text in XPath?

text(): A built-in method in Selenium WebDriver that is used with XPath locator to locate an element based on its exact text value. contains(): Similar to the text() method, contains() is another built-in method used to locate an element based on partial text match.


1 Answers

You can use contains to select the element:

//*[contains(@id, 'f41_txt')]
like image 64
Thomas Jung Avatar answered Sep 21 '22 13:09

Thomas Jung