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.