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)
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.
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'].
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.
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.
you could use contains()
in your XPath predicate. like this:
div[contains(@class, 'header')]
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