How to select elements by class in XPath?

how to select elements by text in xpath

In XPath, you can select elements by class using the contains() function and the @class attribute. Here is an example:


Example of Extracting title tag using requests and BS4.

          //tag[contains(@class,'classname')]
        

In the above XPath, replace tag with the tag name of the element you want to select (e.g. div, p, a, etc.), and replace classname with the name of the class, you want to select.


For example, if you want to select all div elements that have the class name learn, you can use the following XPath:

          //div[contains(@class,'learn')]
        

This will select all div elements that have the class name learn, regardless of the order of the classes in the class attribute.

Additional Resources:

How to select elements by text in Xpath?

Scrapingdog logo

Try Scrapingdog for Free!

Free 1000 API calls of testing.

No credit card required!