I've situation where I can have text in any of the tags(not sure which one is that). Let's take following HTML code snippet for example:
<div .....>
<div ......>
<div ......>
<div ......>
One of the above has text, but not sure which one has it. Let's take following two examples:
In this, div 3 has text:
<div .....>
<div ......>
<div ......>Hello
<div ......>
In this, div 2 has text:
<div .....>
<div ......>Hi
<div ......>
<div ......>
I want to get element for the div which contains ANY text, no matter what it is. Can we write xpath to get an element which has ANY text?
The syntax for locating elements through XPath- Using contains() method can be written as: //<HTML tag>[contains(@attribute_name,'attribute_value')]
We will start to write XPath with //, followed by input tag, then we will use the select attribute, followed by its attribute name like name, id, etc, and then we will choose a value of attribute in single quotes. Here, (1 of 1) means exact match. It indicates that there is only one element available for this XPath.
Locating Strategies- (By XPath- Using text()) In this section, you will learn how to locate a particular web element by XPath- Using text() method. "text() method" is used to identify an element based on the text available on the web page.
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.
You can get all <div>
having direct child non-empty text node(s) like this :
//div[normalize-space(text())]
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