Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML XPath Searching by class and text

Tags:

html

xpath

I want to find all elements in xpath by class and text. I have tried this but it does not work.

//*[contains(@class, 'myclass')]//*[text() = 'qwerty'] 

I am trying to find all elements that have a class of 'myclass' and the text is 'qwert' (these will be span elements)

like image 458
user1786107 Avatar asked May 09 '13 16:05

user1786107


People also ask

How do I search for text in XPath?

So, inorder to find the Text all you need to do is: driver. findElement(By. xpath("//*[contains(text(),'the text you are searching for')]"));

Can we use class in XPath?

Xpath class is defined as a selector that is usually shared by multiple elements in the document means it extracts all the class names. Nodes or lists of nodes are selected using XPath expressions based on property class names. The class name is separated by a Spaces. This token has white space.

How do you XPath a class?

To identify the element with css, the expression should be tagname[class='value'] and the method to be used is By. cssSelector. To identify the element with xpath, the expression should be //tagname[@class='value'].

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.


1 Answers

//span[contains(@class, 'myclass') and text() = 'qwerty'] 

or

//span[contains(@class, 'myclass') and normalize-space(text()) = 'qwerty'] 
like image 85
Tomalak Avatar answered Sep 29 '22 03:09

Tomalak