一、等待机制
1)显式等待
# 显式等待
#方法一:使用time.sleep()函数(不推荐,必须等睡眠时间到后才会执行后面的语句)
a1.get("https://note.youdao.com/signIn/index.html?&callback=https://note.youdao.com/web/&from=web")
time.sleep(10)
a1.find_element(By.XPATH, '/html/body/div[4]/div[2]/div[1]').click()
#方法二:使用WebDriverWait + expected_conditions组合
a1.get("https://note.youdao.com/signIn/index.html?&callback=https://note.youdao.com/web/&from=web")
wait = WebDriverWait(a1, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[4]/div[2]/div[1]'))).click()
2)隐式等待
#隐式等待
a1.get("https://note.youdao.com/signIn/index.html?&callback=https://note.youdao.com/web/&from=web")
# 元素定位隐形等待(多少秒内找到元素就立刻执行,没找到元素就报错)
a1.implicitly_wait(30)
a1.find_element(By.XPATH, '/html/body/div[4]/div[2]/div[1]').click()
二、标签切换
#标签页句柄切换
a1.get("https://baidu.com/")
a1.find_element(By.XPATH, '//*[@id="s-top-left"]/a[1]').click()
a2 = a1.window_handles
a1.switch_to.window(a2[1])
a1.find_element(By.XPATH, '//*[@id="header-link-wrapper"]/li[3]/a').click()
三、弹框确认
1)警告框
#警告框
a1.implicitly_wait(30)
a1.get('https://sahitest.com/demo/alertTest.htm')
a1.find_element(By.XPATH, '/html/body/form/input[1]').clear()
a1.find_element(By.XPATH, '/html/body/form/input[1]').send_keys('hello')
a1.find_element(By.XPATH, '/html/body/form/input[2]').click()
time.sleep(3)
#获取弹窗内的文本内容
print(a1.switch_to.alert.text)
#点击弹窗确定按钮
a1.switch_to.alert.accept()
2)确认框
#确认框
a1.implicitly_wait(30)
a1.get('https://sahitest.com/demo/confirmTest.htm')
a1.find_element(By.XPATH, '/html/body/form/input[1]').click()
time.sleep(2)
#点击弹窗确定按钮
# a1.switch_to.alert.accept()
#点击弹窗取消按钮
a1.switch_to.alert.dismiss()
3)提示框
#提示框
a1.implicitly_wait(30)
a1.get('https://sahitest.com/demo/promptTest.htm')
a1.find_element(By.XPATH, '/html/body/form/input[1]').click()
time.sleep(2)
#弹窗输入内容
a1.switch_to.alert.send_keys('hello')
time.sleep(2)
#弹窗点击确定
a1.switch_to.alert.accept()
四、iframe嵌套页面进入和退出
#iframe嵌套页面进入、退出
a1.implicitly_wait(3)
a1.get('https://sahitest.com/demo/iframesTest.htm')
#获取iframe元素
a2 = a1.find_element(By.XPATH, '/html/body/iframe')
#进入iframe嵌套页面
a1.switch_to.frame(a2)
time.sleep(2)
#进入iframe页面操作元素点击
a1.find_element(By.XPATH, '/html/body/table/tbody/tr/td[1]/a[1]').click()
#退出iframe嵌套页面(返回到默认页面)
a1.switch_to.default_content()
time.sleep(2)
a1.find_element(By.XPATH, '/html/body/input[2]').click()
五、其他操作
1)元素文本内容和获取是否可见
#获取元素文本内容、是否可见
a1.implicitly_wait(30)
a1.get('https://baijiahao.baidu.com/s?id=1869094420533892207')
# 获取元素文本内容 text
text = a1.find_element(By.XPATH, '//*[@id="ssr-content"]/div[2]/div[1]/div[2]/div[2]/div[6]/p').text
print(text)
# 元素是否可见
isdisplay = a1.find_element(By.XPATH, '//*[@id="__SVG_SPRITE_NODE__"]').is_displayed()
print(isdisplay)
2)网页的前进和后退
#网页的前进和后退
a1.implicitly_wait(30)
a1.get('https://www.baidu.com/')
a1.find_element(By.XPATH, '//*[@id="chat-textarea"]').send_keys('dafait')
time.sleep(2)
a1.find_element(By.XPATH, '//*[@id="chat-submit-button"]').click()
time.sleep(2)
#网页后退
a1.back()
time.sleep(2)
#网页前进
a1.forward()
3)嵌入js代码
#嵌入js代码,实现页面一直往下滚动
a1.implicitly_wait(10)
a1.get('https://juejin.cn/')
a2 = a1.find_element(By.XPATH, '//*[@id="juejin"]/div[1]/div/main/div/div[2]/div/aside/div[6]/div/a[2]/p[1]/span')
while True:
time.sleep(2)
a1.execute_script("window.scrollTo(0, document.body.scrollHeight);")