Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding a span with specific content using xpath

I have some span's like this:

<span> 
  <span>foobar</span>  
  <span>thisisatest</span> 
</span>

I'm trying to use xpath to find the span with "thisisatest" in it.

I've tried this:

span[text()='thisisatest']

and it doesn't seem to be working.

like image 513
Joe Bienkowski Avatar asked Apr 16 '12 15:04

Joe Bienkowski


People also ask

Can we use span in XPath?

We can select the text of a span on click with Selenium webdriver. To identify the element with span tag, we have to first identify it with any of the locators like xpath, css, class name or tagname. After identification of the element, we can perform the click operation on it with the help of the click method.

How do I get text inside a span XPath?

You can use By. XPath with the code you have​ Inner text is text between the opening tags and closing tags. For example: <a>I Am Inner Text</a> In above example, texts “I Am Inner Text” between opening and closing tags are called inner text of web element “a”.

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.

How do I navigate to parent node in XPath?

A Parent of a context node is selected Flat element. A string of elements is normally separated by a slash in an XPath statement. You can pick the parent element by inserting two periods “..” where an element would typically be. The parent of the element to the left of the double period will be selected.


2 Answers

You are missing // at the beginning of the XPath.

  • One slash would mean "a span that is a child of a root node".
  • Two slashes mean "find me any span with that text".

//span[text()='thisisatest']

like image 155
Petr Janeček Avatar answered Sep 17 '22 12:09

Petr Janeček


You could try this:

span[span= "thisisatest"] 

All <span> elements that contain at least one <span> element child with the value thisisatest.

Hope it helps!

like image 42
axcdnt Avatar answered Sep 19 '22 12:09

axcdnt