Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xpath - Get HTML element if its class contains some text

Tags:

html

xpath

Can I get an element if its class contains some text?

Example:

<div class="headerbody"></div>   - match (header)
<div class="headerbottom"></div>   - match (header)
<dov class="text"></div>     - not match (header)
like image 505
Roman Bats Avatar asked May 11 '12 12:05

Roman Bats


People also ask

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?

Mastering XPath and CSS Selector for Selenium 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.

How do you find an element that has contains?

contains(): Similar to the text() method, contains() is another built-in method used to locate an element based on partial text match. For example, if we need to locate a button that has “Get started free” as its text, it can be located using the following line of code with Xpath.


1 Answers

you could use contains() in your XPath predicate. like this:

div[contains(@class, 'header')]
like image 60
Pavel Veller Avatar answered Sep 18 '22 19:09

Pavel Veller