"""
======================
@Author:coco
@Time:2019/12/23
@File:等待.py
@software:PyCharm
======================
"""
""""
1.为什么要等待?
你要操作的元素,尚未加载出来。看不见,没法下一步操作。
1)后台数据请求
2)网络很慢
3)页面渲染
2.如何有等?功能测试 什么时候出现了什么操作?智能
3种方式
1)傻等。time.sleep(指定时间秒) 20分钟。
2)智能等待方式:什么时候出现什么时候就停止等待。等待的上限。万一等到了还未等待,timeoutException
1)implicity wait
2)显性等待:所有条件都是明确指出来的。等待条件存在之后,再进行后续的代码执行
等待:WebdriverWait 条件:excepted_condition
等待20秒 检测条件成立的间隔:默认是0.5秒
什么时候要等待?
元素操作之前,都请等一等。
"""
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Chrome()
driver.implicitly_wait(20)
driver.get("http://www.baidu.com")
loc=(By.XPATH,'//div[@id="u1"]//a[@name="tj_login"]')
driver.find_element(*loc).click()
loc=(By.ID,'TANGRAM__PSP_10__footerQrcodeBtn')
WebDriverWait(driver,20).until(EC.visiblity_of_element_located(loc))
time.sleep(1)
driver.find_element_by_xpath('TANGRAM__PSP_10__footerQrcodeBtn').click()
driver.find_element_by_id('TANGRAM__PSP_10__userName').click()
time.sleep(5)
driver.quit()