How to find elements that are either to one or another class attached? Selenium Pyton

Viewed 526

I am using Selenium Python to find buttons on a specific website.

The Problem: I looking for two classes. So I want to create one list containing elements, that contains all elements that are attached either to one or another class. How can I do that? I thought of something like this:

buttons = self.find_elements_by_xpath("//button[@class='classA.classA']" or "//button[@class='classB.classB']")

but of course it does not work.

2 Answers

To create a list of elements with the value of class attribute either as classA or classB you can use either of the following Locator Strategies:

  • Using css_selector:

    buttons = self.driver.find_elements(By.CSS_SELECTOR, "button.classA, button.classB")
    
  • Using xpath:

    buttons = self.driver.find_elements(By.XPATH, "//button[@class='classA' or @class='classB']")
    

Without any additional information your xpath should look like this:

//button[contains(@class,'classA') or contains(@class,'classB')]

buttons = self.find_elements_by_xpath("//button[contains(@class,'classA') or contains(@class,'classB')]")

Take a look at the following example: http://xpather.com/eLDUdgiN

Related