python_selenium 各种等待

该篇博客详细介绍了如何利用Selenium Python库进行网页自动化测试,包括设置隐式和显示等待,以及各种元素定位方法如ID、可见性等。示例中展示了在百度登录页面进行操作的步骤,如点击登录按钮并输入用户名。
摘要由CSDN通过智能技术生成

from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions

driver = webdriver.Chrome()
# 隐式等待: 会等待页面加载完成,最长等待10秒
# driver.implicitly_wait(10)

driver.get("http://www.baidu.com")

driver.find_element(By.ID, "s-top-loginbtn").click()

# 强制等待:
# sleep(3)

# 显示等待:针对某一个/一类元素,每隔0.5秒检查一次,直到这个元素满足条件, 继续往后执行,最长等待30秒时间
wait = WebDriverWait(driver, 30)
# 期望元素存在,但是不一定可见
# (By.ID, "TANGRAM__PSP_11__userName") 通过id查找元素:作为元组传入presence_of_element_located
# wait.until(expected_conditions.presence_of_element_located((By.ID, "TANGRAM__PSP_11__userName")))
# 期望所有元素存在
wait.until(expected_conditions.presence_of_all_elements_located((By.ID, "TANGRAM__PSP_11__userName")))
# 只有有元素可见
# wait.until(expected_conditions.visibility_of_any_elements_located((By.ID, "TANGRAM__PSP_11__userName")))
# 所有元素可见
# wait.until(expected_conditions.visibility_of_all_elements_located((By.ID, "TANGRAM__PSP_11__userName")))
# 单个元素可见
# wait.until(expected_conditions.visibility_of_element_located((By.ID, "TANGRAM__PSP_11__userName")))

driver.find_element(By.ID, "TANGRAM__PSP_11__userName").send_keys("username")
































































  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值