0
I am developing a web automation to insert a list of numbers in a field on a website platform and click recurring within a "for" loop n times according to the dimension of the list. At first the code was right and making the query with the loop in this form:
for i in range(0, len(unique)):
driver.find_element_by_xpath('//*[@id="cph_j0_j1_UcEmpregador1_E_COD_CAMPO"]').clear() #Limpa campo
ActionChains(driver).move_to_element(driver.find_element_by_xpath('//*[@id="cph_j0_j1_UcEmpregador1_E_COD_CAMPO"]')).click().perform() # Seleciona campo
driver.find_element_by_xpath('//*[@id="cph_j0_j1_UcEmpregador1_E_COD_CAMPO"]').send_keys(unique[i]) # digita valor da lista
driver.find_element_by_link_text('Consultar').click()
ActionChains(driver).move_to_element(driver.find_element_by_link_text('Consultar')).click().perform()
rand_time()
if (driver.find_element_by_xpath('//*[@id="cph_j0_j1_GRADITIVO"]/tbody/tr/td').text) != 'Nenhum registro encontrado!':
convenio_update.append(unique[i])
However, at the time of any of the iterations, the program is randomly interrupted by the message: "Staleelementereferenceexception: Stale element Reference: is not Attached to the page Document"
Researching the exception, the best way I could try to treat it, without success, was with the following code:
for i in range(0, len(unique)):
element_1 = driver.find_element_by_xpath('//*[@id="cph_j0_j1_UcEmpregador1_E_COD_CAMPO"]')
element_2 = driver.find_element_by_link_text('Consultar')
try:
element_1.clear() #Limpa campo
ActionChains(driver).move_to_element(element_1).click().perform() # Seleciona campo
element_1.send_keys(unique[i]) # digita valor da lista
element_2.click()
ActionChains(driver).move_to_element(element_2).click().perform()
rand_time()
except NoSuchElementException:
pass
except StaleElementReferenceException: #wait para os dois elementos
element_1 = driver.find_element_by_xpath('//*[@id="cph_j0_j1_UcEmpregador1_E_COD_CAMPO"]')
element_2 = driver.find_element_by_link_text('Consultar')
element_1.clear() #Limpa campo
ActionChains(driver).move_to_element(element_1).click().perform() # Seleciona campo
element_1.send_keys(unique[i])
element_2.click()
ActionChains(driver).move_to_element(element_2).click().perform()
rand_time()
finally:
if (driver.find_element_by_xpath('//*[@id="cph_j0_j1_GRADITIVO"]/tbody/tr/td').text) != 'Nenhum registro encontrado!':
convenio_update.append(unique[i])
Someone would know to point out the error if the program is in the way or has some better alternative?
NOTE: I imported the following modules
# IMPORTANDO BIBLIOTECAS
import pandas as pd
import random
from selenium import webdriver
from selenium.common.exceptions import StaleElementReferenceException, NoSuchElementException
#from selenium.webdriver.common.by import By
#from selenium.webdriver.support.ui import WebDriverWait
#from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time
THANK YOU!!!