I have a def on my OrgPage that it suppose to do a loop while some Xpaths exists. When not, I want to scroll down to display more elements and continue with that iteration. If scrolling down the xpaths are not visible, then stop.
This is my code in OrgPage:
def recoger_datos(self):
# Lista para almacenar los textos de los elementos
element_texts = []
# Contador de iteraciones
i = 1
while True:
XPATH_MIEMBRO = (By.XPATH,f'//*[@id="members-data"]/li[{i}]/a/span[2]/span[2]/span[1]/span[1]')
XPATH_RANGO = (By.XPATH, f'//*[@id="members-data"]/li[{i}]/a/span[2]/span[2]/span[3]')
try:
# Intenta encontrar el elemento
element_miembro = self.find_element(XPATH_MIEMBRO)
#logging.info(XPATH_MIEMBRO)
element_rango = self.find_element(XPATH_RANGO)
#logging.info(XPATH_RANGO)
# Obtiene el texto del elemento
numero = str(i)
text_miembro = element_miembro.text
logging.info(numero + " " + text_miembro)
text_rango = element_rango.text
logging.info(numero + " " + text_rango)
# Almacena los textos en un diccionario
element_dicts = {"Miembro": text_miembro, "Rango": text_rango}
# Incrementa el contador para la siguiente iteración
i += 1
except NoSuchElementException:
break
“find_element” function is located in my BasePage. This is the code:
#Find element
def find_element(self, locator):
try:
element = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(locator))
assert element is not None, f"No se encontró el elemento con el locator: {locator}"
return element
except (TimeoutException, NoSuchElementException) as e:
# Si el elemento no se encuentra, imprime el error y lanza una excepción
logging.info(f"Error al encontrar el elemento: {locator}")
raise
When executing this, I receive the error: FAILED test_main.py::test_recoger_datos_ccbe – selenium.common.exceptions.TimeoutException: Message:
My last loggin.info string was that XPATH was not find. This loggin.info is part of “find_element” function.
Im not sure if except NoSuchElementException: from recoger_datos(self) is doing his job. I suppose that is needed for adding a scroll down function at the end of recoger_datos(self) and then make the While loop “continue“?