Python让selenium对浏览器通知做出反应(Chrome)

2024-04-25 09:57:23 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有办法让selenium监听和响应浏览器通知,如电子邮件或whatsapp web通知


Tags: web电子邮件selenium浏览器whatsapp办法
1条回答
网友
1楼 · 发布于 2024-04-25 09:57:23

是的,当然!我给你举了一个例子,我故意让谷歌创建一个假的浏览器通知(只是为了演示)。您只需检查alert.dismiss()

我们开始了:

from selenium import webdriver
import time

driver = webdriver.Chrome("<your path to chromedriver>")
driver.get("http://google.com")

# Creating a fake notification. Just ignore this.
driver.execute_script("window.alert('This is an alert');")
time.sleep(2)

# Now here's what you are looking for
alert = driver.switch_to_alert()
alert.dismiss()

相关问题 更多 >