Get input tag element with given text value via xpath

How can I select through xpath all the input elements in the document that have the given value entered into them.

For example, if I go to Google and type “hello world”, how do I get all input tags that have “hello world” typed into them?

Playing with things like the one below didn't pay off, because the value in the text box is not part of the document.

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

It should be pretty simple, but I'm amazingly stuck.

+7
source share
4 answers

Your x-path expression should look for inputs with an attribute value with 'hello world'

This is because where the value is entered, not the inner text of the element.

The actual html element will look like this:

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

XPATH expression should look like this:

 //input[@value = 'hello world'] 
+15
source

An alternative without jQuery to get input that contains the target text:

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

This will find input even if the user enters "hello world number 7"

+2
source

You did not specify a language, but in order to get user input, it must be some kind of javascript. In jQuery you can do this:

 $("input:contains('hello world')").val() 

For more details see jQuery docs in contains() selector and val

0
source

This also works:

 //input[@type='text'][@value='hello world'] 
0
source

Source: https://habr.com/ru/post/910968/


All Articles