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.