Search code examples
pythonseleniumselenium-webdriverfavorite

Python Selenium: find element for favorite/unfavorite button


I'm trying to find all elements on a page that have not already been clicked, or 'favorited.'

Here is the html for an element that is favorited:

<a class="button-fave unfavorited-button favorited-button" rel="78853399" alt="Add to favorites">
    <div class="button-spinner"></div>
    <span class="status-text">Favorite</span>
</a>

Here is the html code for when the element is not favorited:

<a class="button-fave unfavorited-button" rel="78853399" alt="Add to favorites">
    <div class="button-spinner"></div>
    <span class="status-text">Favorite</span>
</a>

I have tried:

driver.find_element_by_class_name('button-fave unfavorited-button')

but I get the following:

The given selector button-fave unfavorited-button is either invalid or does not result in a WebElement. The following error occurred: InvalidSelectorError: Compound class names not permitted

The following works, but it doesn't discern between favorited elements and unfavorited elements:

driver.find_element_by_class_name('button-fave') 

Solution

  • You can find all a elements not having favorited-button class:

    driver.find_elements_by_css_selector("a:not(.favorited-button)")