2

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.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Tknoobs
  • 169
  • 10
  • are those two classes always present or is it like like they interchange in some pages so that only one is present sometimes? If both are present always then it's better to create two variables so that you can use whichever one you want. – Ananth Jan 24 '21 at 16:27
  • Help us to help you - Please improve your question, so that we can reproduce your issue. How to create [mcve] Thanks ---- (url, source code what to find, expected output,.. would be great ) – HedgeHog Jan 24 '21 at 17:21
  • An html example of your page would be extremely helpful. Any answers without it can only be guessing at the problem. – Michiel Bugher Jan 24 '21 at 17:39
  • @Ananth Both classes are present, but I need to find the button in the order they are appearing. – Tknoobs Jan 25 '21 at 09:02
  • @HedgeHog The URL is the one that pops up when you press on the button on an Instagram Profile to see its followers. I want to find all buttons the ones I already followed and the ones I can follow in the right order – Tknoobs Jan 25 '21 at 09:04

2 Answers2

3

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']")
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

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

HedgeHog
  • 22,146
  • 4
  • 14
  • 36