比如有这个页面:点击按钮2s后替换div
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>selenium等待条件</title>
</head>
<body>
<script>
function f() {
window.setTimeout("populate()",2000)
}
function populate() {
document.f1.t1.value = "populated";
document.getElementById("id1").innerHTML = "<div id='id2'>点击div替换了</div>"
}
</script>
<form name="f1">
<input type="text" name="t1">
<input type="button" id="btn" value="Clike me" onclick="f()">
<div id="id1">
</div>
</form>
</body>
</html>
第一种情况,如果在定位页面元素时不加等待时间
from selenium import webdriver
from time import sleep
import os
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class TestCase(object):
def __init__(self):
self.driver = webdriver.Chrome()
path = 'file:///'+os.path.abspath('test_wait.html')
print(path)
self.driver.get(path)
def test(self):
self.driver.find_element_by_id('btn').click()
print(self.driver.find_element_by_id('id2').text)
if __name__ == '__main__':
case = TestCase()
case.test()
执行时会找不到页面元素报错
"D:\test tool\pythonProjects\pythonStady\venv\Scripts\python.exe" "D:/test tool/pythonProjects/pythonStady/selenium/seleniumWait.py"
file:///D:\test tool\pythonProjects\pythonStady\selenium\test_wait.html
Traceback (most recent call last):
File "D:/test tool/pythonProjects/pythonStady/selenium/seleniumWait.py", line 26, in <module>
case.test()
File "D:/test tool/pythonProjects/pythonStady/selenium/seleniumWait.py", line 21, in test
print(self.driver.find_element_by_id('id2').text)
File "D:\test tool\pythonProjects\pythonStady\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "D:\test tool\pythonProjects\pythonStady\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "D:\test tool\pythonProjects\pythonStady\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "D:\test tool\pythonProjects\pythonStady\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="id2"]"}
(Session info: chrome=91.0.4472.114)
Process finished with exit code 1
第二种情况:显式等待时间较短
from selenium import webdriver
from time import sleep
import os
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class TestCase(object):
def __init__(self):
self.driver = webdriver.Chrome()
path = 'file:///'+os.path.abspath('test_wait.html')
print(path)
self.driver.get(path)
def test(self):
self.driver.find_element_by_id('btn').click()
#显式等待
wait = WebDriverWait(self.driver,1)
wait.until(EC.text_to_be_present_in_element((By.ID,'id2'), '点击div替换了'))
print(self.driver.find_element_by_id('id2').text)
if __name__ == '__main__':
case = TestCase()
case.test()
执行会操时
"D:\test tool\pythonProjects\pythonStady\venv\Scripts\python.exe" "D:/test tool/pythonProjects/pythonStady/selenium/seleniumWait.py"
file:///D:\test tool\pythonProjects\pythonStady\selenium\test_wait.html
Traceback (most recent call last):
File "D:/test tool/pythonProjects/pythonStady/selenium/seleniumWait.py", line 26, in <module>
case.test()
File "D:/test tool/pythonProjects/pythonStady/selenium/seleniumWait.py", line 20, in test
wait.until(EC.text_to_be_present_in_element((By.ID,'id2'), '点击div替换了'))
File "D:\test tool\pythonProjects\pythonStady\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Process finished with exit code 1