[Python] Problemas de StaleElementReferenceException em Selenium com Python

Discussão em 'Python' iniciado por Stack, Novembro 7, 2024.

  1. Stack

    Stack Membro Participativo

    sou novo aqui no fórum,espero que possam me ajudar, estou criando uma aplicação com selenium utilizando python para coletar documentos em um site específico. os documentos são carregados para download via javascript e então meu loop só pega o primeiro documento e ao tentar pegar o segundo dá erro StaleElementReferenceException, segue abaixo uma amostra do html e o bloco de código que estou usando. Obs.: Estou usando python 3.6 e driver do chrome. Eu quero pegar apenas os links da segunda coluna da tabela.

    <table id="GRDDOCS" border="0" cellpadding="0" cellspacing="0" width="780" class="Texto" style="z-index:100;width:780;height:20;">
    <tbody><tr>
    <tr>
    <td valign="middle" align="left" nowrap="" width="90" class="texto1">&nbsp;</td>
    <td valign="middle" align="left" nowrap="" width="300" class="texto1">
    <a href="#" onclick="return SubmitClickConfirm('GRDDOCS','6_1', true, '')"></a>
    <a href="javascript:void(0)" onclick="LINK11; return false" id="LINK11" name="LINK11" style="z-index:100;font-weight:bold;font-size:9px;text-decoration:none;color:#FF0000;">documento.jpg</a>
    </td>
    <td valign="middle" align="left" nowrap="" width="170" class="texto1">
    <a href="javascript:void(0)" onclick="LINK12; return false" id="LINK12" name="LINK12" style="z-index:100;">Excluir</a>
    </td>
    <td valign="middle" align="left" nowrap="" width="170" class="texto1">&nbsp;</td>
    <td></td>
    </tr>
    <tr>
    <td valign="middle" align="left" nowrap="" width="90" class="texto2">&nbsp;</td>
    <td valign="middle" align="left" nowrap="" width="300" class="texto2">
    <a href="#" onclick="return SubmitClickConfirm('GRDDOCS','7_1', true, '')"></a>
    <a href="javascript:void(0)" onclick="LINK13; return false" id="LINK13" name="LINK13" style="z-index:100;font-weight:bold;font-size:9px;text-decoration:none;color:#FF0000;">documento02.jpg</a>
    </td>
    <td valign="middle" align="left" nowrap="" width="170" class="texto2">
    <a href="javascript:void(0)" onclick="LINK14; return false" id="LINK14" name="LINK14" style="z-index:100;">Excluir</a>
    </td>
    <td valign="middle" align="left" nowrap="" width="170" class="texto2">&nbsp;</td>
    <td></td>
    </tr>
    <tr>
    <td valign="middle" align="left" nowrap="" width="90" class="texto1">&nbsp;</td>
    <td valign="middle" align="left" nowrap="" width="300" class="texto1">
    <a href="#" onclick="return SubmitClickConfirm('GRDDOCS','8_1', true, '')"></a>
    <a href="javascript:void(0)" onclick="LINK15; return false" id="LINK15" name="LINK15" style="z-index:100;font-weight:bold;font-size:9px;text-decoration:none;color:#FF0000;">documento03.jpg</a>
    </td>


    Bloco de código:

    if driver.find_element_by_id("GRDDOCS"):
    table_id = driver.find_element_by_id("GRDDOCS")
    rows = table_id.find_elements(By.TAG_NAME, "tr")
    for row in rows:
    col = row.find_elements(By.TAG_NAME, "td")[1]
    if col.find_elements(By.TAG_NAME, "a"):
    prop = driver.find_element_by_link_text(col.text)
    driver.execute_script("arguments[0].click();", prop)
    time.sleep(5)


    Bom dia galera, fiz as seguintes alterações, adicionei uma espera explicita após pegar o primeiro documento para verificar se os elementos retornam para a página e faço print caso encontre e tento pegar o documento de novo mas sem sucesso, alguem pode me ajudar? segue código atualizado abaixo:

    if driver.find_element_by_id("GRDDOCS"):
    table_id = driver.find_element_by_id("GRDDOCS")
    rows = table_id.find_elements(By.TAG_NAME, "tr")
    for row in rows:
    col = row.find_elements(By.TAG_NAME, "td")[1]
    if col.find_elements(By.TAG_NAME, "a"):
    prop = self.driver.find_element_by_link_text(col.text)
    self.driver.execute_script("arguments[0].click();", prop)
    element = WebDriverWait(self.driver, 120).until(
    EC.presence_of_element_located((By.ID, "GRDDOCS"))
    )
    time.sleep(5)
    if element.is_displayed:
    self.logger.info(f"Elemento visível:
    {element.is_displayed()}")
    self.driver.execute_script("arguments[0].click();", prop)

    Continue reading...

Compartilhe esta Página