警告框处理
在WebDriver中处理JavaScript所生成的alert、confirm以及prompt十分简单,具体做法是使用 switch_to.alert 方法定位到 alert/confirm/prompt,然后使用text/accept/dismiss/ send_keys等方法进行操作
-
text:返回 alert/confirm/prompt 中的文字信息
-
accept():接受现有警告框
-
dismiss():取消现有警告框
-
send_keys(“haha”):发送文本至警告框
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.implicitly_wait(5)
driver.get("D:\\test\script\study\seleniumStu\day6\警告框.html")
# # 触发对话框
# driver.find_element_by_id("bu1").click()
# al = driver.switch_to.alert
# time.sleep(3)
# al.accept() # 确认对话框
# # 触发确认框
# driver.find_element_by_id("bu2").click()
# al = driver.switch_to.alert
# al.accept() # 确认对话框
# driver.find_element_by_id("bu2").click()
# al.dismiss() # 取消对话框
# 触发提示框
driver.find_element_by_id("bu3").click()
al = driver.switch_to.alert
al.send_keys("口罩太贵了")
al.accept() # 确认对话框
time.sleep(3)
driver.quit()
窗口截图
webdriver 提供了 get_screenshot_as_file()函数来截取当前窗口
from selenium import webdriver标签:技巧,对话框,selenium,driver,al,accept,alert,集合,find From: https://www.cnblogs.com/junfanzy/p/17292718.html
from time import sleep
driver = webdriver.Chrome()
driver.get('http://www.baidu.com')
# 截屏,截取整个页面
driver.get_screenshot_as_file("./all.png")
# 截屏,截取单个元素
ele = driver.find_element_by_id('kw')
ele.screenshot("./ele.png")
driver.quit()