driver.find_elements_by_xpath search on only one class

Viewed 169

In my page to scrape I have some elements like

<a href='xxxx' class='main-class class2 class3'>

end others elements like

<a href='xxxx' class='main-class class4 class5'>

I want to retrieve all these elements, so I did

elems = driver.find_elements_by_xpath("//a[@class='main-class']")

but it doesn't retrieve anything. I can't search on one class only. For the moment, it is working only if I indicate the three classes.

Thank you

1 Answers

You should use the following XPath:

elems = driver.find_elements_by_xpath("//a[contains(@class,'main-class')]")

This will give you all the elements containing this class

Related