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.
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”.
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.
Using the XPath contains() function, we can extract all the elements on the page that match the provided text value.
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']
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With