Home > Back-end >  Python Webscrape - Selenium 'CLASS_NAME' - 'not' to include all elements
Python Webscrape - Selenium 'CLASS_NAME' - 'not' to include all elements

Time:08-03

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.

CodePudding user response:

Try to use XPath:

driver.find_elements(By.XPATH, "//*[contains(@class, 'xxxx') and not(contains(@class, 'xxxxyy'))]")
  • Related