Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get input tag element with a given text value via xpath

Tags:

xpath

How can I select, via xpath, all input elements in a document that have a given value typed into them.

For instance, if I go to Google and type in "hello world", how do I get all input tags that have "hello world" typed into them?

Playing around with things like below haven't paid off, since the value in the text field isn't really part of the document.

document.evaluate("//input[text() = 'hello world']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue

Should be pretty simple, but I'm surprisingly stuck.

like image 373
secureboot Avatar asked Mar 17 '12 05:03

secureboot


People also ask

How do you write XPath for input text?

Note: Use Ctrl+F to write XPath in the elements tab as shown below. As seen above, a simple XPath is used to locate the firstName tab. Based on the XPath syntax, first use // which means anywhere in the document. Here, input is the tag name that has an id attribute with the value “usernamereg-firstname”.

What is text () in XPath?

XPath text() function is a built-in function of the Selenium web driver that locates items based on their text. It aids in the identification of certain text elements as well as the location of those components within a set of text nodes. The elements that need to be found should be in string format.

Which XPath function is used to extract all the elements which matches a particular text value?

Using the XPath contains() function, we can extract all the elements on the page that match the provided text value.


2 Answers

Your x-path expression should searching for inputs that have the value attribute with 'hello world'

This is because that's where the value gets put into, not the inner text of the element.

The actual html element would look like:

<input type='text' value='hello world' />

The XPATH expression should look like:

//input[@value = 'hello world']
like image 116
Oved D Avatar answered Nov 02 '22 17:11

Oved D


An alternative without jQuery for getting the input which contains the target text is:

//input[contains(@value, 'hello world')]

This would find the input even if the user types "hello world number 7"

like image 25
Rich Flickstein Avatar answered Nov 02 '22 18:11

Rich Flickstein