mgs
mgs

Reputation: 23

Python get selenium to react to browser notifications (Chrome)

Is there any way to get selenium to listen and react to browser notifications like an email or a whatsapp web notification?

Upvotes: 2

Views: 690

Answers (1)

Abhay Salvi
Abhay Salvi

Reputation: 1119

Yeah sure! I am presenting you an example where i intentionally make google create a fake browser notification (just for a demo). You just check the alert.dismiss() line.

Here we go :

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()

Upvotes: 1

Related Questions