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

0

I'm having a problem fetching an item from within a page, this item loads after loading the page via ajax or iframe , is there any way to to create a condition for the script to wait until the item appears?

To exemplify 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 id_2 which 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>functionsleep(milliseconds){varstart=newDate().getTime();for(vari=0;i<1e7;i++){if((newDate().getTime()-start)>milliseconds){break;}}}$(document).ready(function(){console.log('jsstart');sleep(5000);jQuery('<div>DIV2</div>',{id:'id_2',}).appendTo('#content');console.log('jsDone');});</script></head><body><div>Mainpage</div><divid='content'><divid="id_1">DIV 1</div>
    </div>
</body>
</html>

This is the HTML I have 2 div if you search for the div id_1 it finds no problem, but the div id_2 displayed 5 seconds after page loading is not found by selenium even though I determine the wait time of 10 seconds.

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

    
asked by anonymous 13.07.2018 / 13:52

1 answer

0

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

If you give a preview element or display source code you will see that the new element is without%

Example:

<!doctypehtml><htmllang="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 the Selenium code I did this:

#!/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()

Result:

    
14.07.2018 / 17:05