Python Webscrape – Selenium 'CLASS_NAME' – 'not' to include all elements

Question:

PYTHON – For Webscraping – is there a way to use Selenium to find an element by CLASS_NAME – but only return the elements under class name ‘xxxx’ and not elements under class name ‘xxxxyy’.

This code returns ALL elements (inclusive) with CLASS_NAME of ‘xxxx’…..and includes CLASS_NAME of ‘xxxxyy’.

driver.find_elements(By.CLASS_NAME, 'xxxx')

This is my attempt – which returns nothing.

driver.find_elements((By.CLASS_NAME, 'xxxx') and (not(By.CLASS_NAME, 'xxxxyy')))

Thankyou.

Asked By: GDog

||

Answers:

Try to use XPath:

driver.find_elements(By.XPATH, "//*[contains(@class, 'xxxx') and not(contains(@class, 'xxxxyy'))]")
Answered By: Curious koala
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.