0

I have a problem:

tag = driver.find_elements_by_xpath('//*[@class="du4w35lb k4urcfbm l9j0dhe7 sjgh65i0"]')
tag = driver.find_elements_by_class_name("du4w35lb k4urcfbm l9j0dhe7 sjgh65i0")

I thought both would work the same but when I ran them, the first one returned some elements but the second one returns an empty list.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    The issue is that this is a compound class name and is not supported by selenium. Hence, the better (and only) way to access the element is via the `xpath`. For more clarification see this question [here](https://stackoverflow.com/questions/37771604/selenium-compound-class-names-not-permitted). – trotta Jan 29 '21 at 16:55
  • You're trying to match mutliple class names. takea. look at https://stackoverflow.com/questions/51204668/how-to-find-element-that-has-multiple-class-in-selenium – DMart Jan 29 '21 at 16:56
  • This might help: https://stackoverflow.com/questions/47267591/cssselector-vs-xpath-for-selenium/54605738 – Cillian Collins Jan 29 '21 at 16:56
  • unbalanced quotes in the second one. – pcalkins Jan 29 '21 at 18:54

1 Answers1

0

find_elements_by_class_name(class_name)

find_elements_by_class_name(class_name) finds elements by class name and returns a list with elements if any was found. An empty list if not. It is defined as:

def find_elements_by_class_name(self, name):
    """
    Finds elements by class name.

    :Args:
     - name: The class name of the elements to find.

    :Returns:
     - list of WebElement - a list with elements if any was found.  An
       empty list if not

    :Usage:
        ::

            elements = driver.find_elements_by_class_name('foo')
    """
    warnings.warn("find_elements_by_* commands are deprecated. Please use find_elements() instead")
    return self.find_elements(by=By.CLASS_NAME, value=name)
    

It is worth to be noted that find_elements_by_class_name() accepts a single class as an argument. Passing multiple classes you will face the error as:

Message: invalid selector: Compound class names not permitted

An example:

tags = driver.find_elements(By.CLASS_NAME, "du4w35lb")

You can find a relevant detailed discussion in Invalid selector: Compound class names not permitted error using Selenium


find_elements_by_xpath(xpath)

find_elements_by_xpath(xpath) finds multiple elements by xpath and returns a list with elements if any was found. An empty list if not. It is defined as:

def find_elements_by_xpath(self, xpath):
    """
    Finds multiple elements by xpath.

    :Args:
     - xpath - The xpath locator of the elements to be found.

    :Returns:
     - list of WebElement - a list with elements if any was found.  An
       empty list if not

    :Usage:
        ::

            elements = driver.find_elements_by_xpath("//div[contains(@class, 'foo')]")
    """
    warnings.warn("find_elements_by_* commands are deprecated. Please use find_elements() instead")
    return self.find_elements(by=By.XPATH, value=xpath)
        

An example:

tags = driver.find_elements(By.XPATH, "//*[@class='du4w35lb k4urcfbm l9j0dhe7 sjgh65i0']")
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352