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)
So, inorder to find the Text all you need to do is: driver. findElement(By. xpath("//*[contains(text(),'the text you are searching for')]"));
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.
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'].
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.
//span[contains(@class, 'myclass') and text() = 'qwerty']
or
//span[contains(@class, 'myclass') and normalize-space(text()) = 'qwerty']
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