第10节 Selenium 显式等待(WebDriverWait)使用介绍

比如有这个页面:点击按钮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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值