Skip to content Skip to sidebar Skip to footer

Loop Through Span Elements In Selenium Python, AttributeError: 'list' Object Has No Attribute 'click'

I want to loop through a list of span elements and make Selenium click all span elements available. Currently I'm getting an AttributeError: 'list' object has no attribute 'click'.

Solution 1:

Here is your problem: method self._find_all(self, locator) returns a list of elements, so instead of use it as

def _click_all(self, locator):
    self._find_all(locator).click()

you should do

def _click_all(self, locator):
    for element in self._find_all(locator):
        element.click()

Also note that if clicking target element triggers page refresh/navigation to new page, you will get StaleElementReferenceException, so _click_all() might be applied to list of elements that performs some actions on static page

Update

from selenium.webdriver.support.ui import WebDriverWait as wait

def _click_all(self, locator):
    counter = len(self._find_all(locator))
    for index in range(counter):
        self._find_all(locator)[index].click()
        self.driver.back()
        wait(self.driver, 10).until(lambda driver: len(self._find_all(locator)) == counter)

Post a Comment for "Loop Through Span Elements In Selenium Python, AttributeError: 'list' Object Has No Attribute 'click'"