Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xpath to find out element which has ANY text

Tags:

xpath

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?

like image 982
Alpha Avatar asked Jul 30 '14 18:07

Alpha


People also ask

How find XPath text contains?

The syntax for locating elements through XPath- Using contains() method can be written as: //<HTML tag>[contains(@attribute_name,'attribute_value')]

How will you write XPath if the tag has only text?

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.

Can we use text () in 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.

What is text () function 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.


1 Answers

You can get all <div> having direct child non-empty text node(s) like this :

//div[normalize-space(text())]
like image 195
har07 Avatar answered Sep 19 '22 05:09

har07