# 此弹窗是浏览器自带的弹窗,不是html中的元素
from selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainsimport timefrom selenium.webdriver.support.select import Select driver = webdriver.Firefox()url = "https://www.baidu.com"driver.get(url)time.sleep(3) mouse = driver.find_element("link text", "设置")ActionChains(driver).move_to_element(mouse).perform()time.sleep(0.5)driver.find_element("link text", "搜索设置").click()time.sleep(1) # 先定位到下拉框s = driver.find_element_by_id("nr")Select(s).select_by_visible_text("每页显示20条")s.click()time.sleep(1) # 点击保存按钮,此时会弹出确认提示框driver.find_element_by_class_name("prefpanelgo").click()time.sleep(0.5) # 获取弹窗a = driver.switch_to_alert()
# 或者# a = driver.switch_to.alert
print(a.text) # 打印弹窗的文字内容 # 1、accept() 弹框的确定按钮a.accept() # 点确定按钮 # 2、dismiss() 弹框的取消按钮a.dismiss() # 点取消按钮 # 3、弹框若有输入框,需输入内容a.send_keys("xxxx")
原文地址:https://www.cnblogs.com/dwdw/p/9998650.html
时间: 2024-11-09 09:21:36