I'm trying to click on each item in a span using Selenium. Is it possible to specify this in a for loop and how could I do this?
This is how I currently click on the first span item:
_open_actieve_polis = {"by": By.CSS_SELECTOR, "value": "td:nth-child(2)"}
self._click(self._open_actieve_polis)
in the base page this is how i implemented the _click method.
def _find(self, locator):
return self.driver.find_element(locator["by"], locator["value"])
def _click(self, locator):
self._find(locator).click()
these are the other span items I want to click:
tbody > tr:nth-child(2) > td:nth-child(2) > span > a
tbody > tr:nth-child(3) > td:nth-child(2) > span > a
tbody > tr:nth-child(4) > td:nth-child(2) > span > a
tbody > tr:nth-child(5) > td:nth-child(2) > span > a
tbody > tr:nth-child(6) > td:nth-child(2) > span > a
This is what I tried after applying feedback:
This in a method:
for link in self._open_actieve_polis:
self._click_all(link)
self.driver.back()
I declared the locator in an attribute:
_open_actieve_polis = ({"by": By.CSS_SELECTOR, "value": "td:nth-child(2)"})
the following in base page:
def _find_all(self, locator):
return self.driver.find_elements(locator["by"], locator["value"])
def _click_all(self, locator):
self._find_all(locator).click()
is currently resulting in:
line 18, in _find_all
return self.driver.find_elements(locator["by"], locator["value"])
TypeError: string indices must be integers
_click()method