Home > Enterprise >  Python Selenium - Interact with the next WebElement object in a list
Python Selenium - Interact with the next WebElement object in a list

Time:10-25

I have a list of web elements like this:

menu = self.driver.find_elements(By.CSS_SELECTOR, '[]')

For all the web elements contained in list menu I have to perform a click, but with my code below the first element in the list is clicked n times instead of clicking one time on all the elements in the list:

menu = self.driver.find_elements(By.CSS_SELECTOR, '[]')
for index, value in enumerate(menu):
    value = self.driver.find_element(By.CSS_SELECTOR, '[]')
    value.click()

I know that I'm not looping correctly, but I don't know what I'm doing wrong. Thanks for help.

CodePudding user response:

Your question is missing debugging details, but I guess the following should work:

menu = self.driver.find_elements(By.CSS_SELECTOR, '[]')
for value in menu:
    value.find_element(By.CSS_SELECTOR, '[]').click()

In case clicking the menu options performs refreshing the page the following should help:

menu = self.driver.find_elements(By.CSS_SELECTOR, '[]')
for value in menu:
    value.find_element(By.CSS_SELECTOR, '[]').click()
    menu = self.driver.find_elements(By.CSS_SELECTOR, '[]')
  • Related