与switch_to.window 和 switch_to.frame 相比,switch_to.alert的alert方法使用了@property 装饰器,所以在使用时alert被当成属性调用。

演示如下

from selenium import webdriver

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.baidu.com")

# 模拟一个弹窗
js = "alert('我弹出来了!')"
driver.execute_script(js)

# 切换弹窗
al = driver.switch_to.alert     # alert以属性的方式调用

# 获取弹窗文本内容
print(al.text)

# 点击弹窗确定按钮
al.accept()

# 点击弹窗取消按钮
# al.dismiss()

相关文章:

  • 2021-08-31
  • 2022-02-28
  • 2022-12-23
  • 2021-06-03
  • 2021-08-18
  • 2021-07-30
  • 2021-09-13
猜你喜欢
  • 2022-01-08
  • 2021-12-06
  • 2022-12-23
  • 2021-11-29
  • 2021-09-07
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案