Python webdriver Selenium does not find the item on the page after loading

Asked

Viewed 1,152 times

0

I am having a problem when searching for an item inside a page, this item is loaded after loading the page via ajax or iframe, there is some way to create a condition for the script to wait until the item appears?

To illustrate my problem I did the following test:

Python:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
browser = webdriver.Firefox()
browser.get("http://localhost/test_time.php")
delay = 10 # seconds
try:
    myElem = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'id_2'))) 
    print ("Elememento encontrado")
except TimeoutException:
    print ('Nao foi dessa vez :(')
    pass

I search for the id_2 that is displayed 5 seconds after the page load is completed by javascript

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Teste Python</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
        function sleep(milliseconds) {
            var start = new Date().getTime();
            for (var i = 0; i < 1e7; i++) {
                if ((new Date().getTime() - start) > milliseconds){
                break;
                }
            }
        }
        $( document ).ready(function() {
            console.log('js start');     
            sleep(5000);
            jQuery('<div> DIV 2 </div>', {id: 'id_2', }).appendTo('#content');                      
            console.log('js Done');                 
        });
    </script>    
</head>
<body>
    <div>Main page</div>
    <div id='content'>
        <div id="id_1">DIV 1</div>
    </div>
</body>
</html>

This is the HTML I have 2 div if you search for div id_1 he finds without any problem, but the div id_2 which is displayed 5 seconds after page loading is not found by Selenium even if I determine the Wait time of 10 seconds.

I would like a light to find a solution to this problem.

1 answer

1


The way you are creating the element with Jquery is not creating the attribute id.

If you give a inspect element or display page source code you will see that the new element is without id.

inserir a descrição da imagem aqui

Example:

<!doctype html>
<html lang="pt_BR">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Exemplo Selenium</title>
</head>

<body>
    <div>Main page</div>
    <div id='content'>
        <div id="id_1">DIV 1</div>
    </div>

    <!-- CDN do Jquery (Ajax) -->
    <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
        crossorigin="anonymous"></script>

    <!-- Script -->
    <script>
        function sleep(ms) {
            return new Promise(resolve => setTimeout(resolve, ms));
        }

        async function criarElementoJquery() {
            // Criando um novo elemento.
            let novo = $("<div></div>");
            // Definindo o texto do elemento.
            novo.text("DIV 2");
            // definindo um atributo para o elemento.
            novo.attr("id", "id_2");
            // Aguardando 5 segundo para criar o elemento.
            await sleep(5000);
            // Adicionando o elemento.
            $("#content").append(novo);
        }

        async function criarElementoJS() {
            // Criando um novo elemento
            let novo = document.createElement("div");
            // Definindo um atributo do tipo id para o elemento.
            novo.setAttribute("id", "id_3");
            // Inserindo um valor no elemento.
            novo.append("DIV 3");
            // Aguardando 10 segundo para criar o novo elemento.
            await sleep(10000)
            // Selecionando o elemento e adicionando o conteúdo.
            document.getElementById("content").appendChild(novo);
        }

        window.onload = function () {
            console.log("Página carregada")
            criarElementoJquery();
            criarElementoJS();
        }

    </script>
</body>

</html>

In Selenium’s code I did so:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Buscando elemento pelo id com Selenium"""
from selenium import webdriver

# Definindo o navegador que fará a conexão.
driver = webdriver.Firefox()

# Endereço que será acessado.
driver.get('http://127.0.0.1:5000/')

# Tempo que o Selenium vai ficar verificando a página.
driver.implicitly_wait(30)

try:
    # Buscando elemento pela id.
    elemento = driver.find_element_by_id('id_2')
except Exception as e:
    print('Nao foi dessa vez :(', e)
else:
    print('Elememento encontrado:')
    print(elemento)
finally:
    # Fechando a janela do navegador
    driver.quit()

Upshot:

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.