How to handle notifications in Python with Selenium Chrome WebDriver

+1 vote

How can we handle Selenium Chrome WebDriver notifications in Python?

I am trying to dismiss/accept alert and active element. Also, all the Google search results are driving me to Java solution which I do not really need.

May 11, 2018 in Selenium by Martin
• 4,320 points
14,421 views

1 answer to this question.

+1 vote

Below will help you:

You can disable the browser notifications, using chrome options.

chrome_options = webdriver.ChromeOptions()

prefs = {"profile.default_content_setting_values.notifications" : 2}

chrome_options.add_experimental_option("prefs",prefs)

driver = webdriver.Chrome(chrome_options=chrome_options)

 
answered May 11, 2018 by Samarpit
• 5,910 points
Not worked for me
Hey @Imran. Can you share the code that you are using?
@Imran, yes please do send your code or the error you are getting?

its not working for me can anyone please help me?

i am using ChromeDriver 78.0.3904.105
and latest Selenium version  

from selenium import webdriver
import time

from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

chrome_options = webdriver.ChromeOptions()

prefs = {"profile.default_content_setting_values.notifications": 2}

chrome_options.add_experimental_option("prefs", prefs)

driver = webdriver.Chrome(chrome_options=chrome_options)

hashtag = "anytag"
driver = webdriver.Chrome()
driver.get("https://www.instagram.com/accounts/login/?source=auth_switcher")
time.sleep(3)
driver.find_element_by_name('username').send_keys("user")
driver.find_element_by_name('password').send_keys("pass")
driver.find_element_by_xpath("//text()[.='Log In']/ancestor::div[1]").click()
time.sleep(3)
driver.find_element_by_xpath('/html/body/div[4]/div/div[1]/div[3]/button[@class="aOOlW  bIiDR  "]').click()
time.sleep(4)
driver.find_element_by_xpath("//input[@type='text'][@placeholder='Search']").send_keys(hashtag)

Hey @Khan try this:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs", prefs)
chrome_options.add_argument("start-maximized")
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=r'C:\path\to\chromedriver.exe')
hashtag = "anytag"
driver.get("https://www.instagram.com/accounts/login/?source=auth_switcher")
time.sleep(3)
driver.find_element_by_name('username').send_keys("user")
driver.find_element_by_name('password').send_keys("pass")
driver.find_element_by_xpath("//text()[.='Log In']/ancestor::div[1]").click()
time.sleep(3)
driver.find_element_by_xpath('/html/body/div[4]/div/div[1]/div[3]/button[@class="aOOlW  bIiDR  "]').click()
time.sleep(4)
driver.find_element_by_xpath("//input[@type='text'][@placeholder='Search']").send_keys(hashtag)
time.sleep(6)
driver.quit()

Related Questions In Selenium

0 votes
1 answer
+1 vote
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,500 points
8,356 views
0 votes
2 answers

Can anyone help me that how to run Selenium WebDriver test cases in Chrome?

You first need to download chrome driver ...READ MORE

answered Aug 26, 2019 in Selenium by Abha
• 28,140 points
1,967 views
0 votes
1 answer

How to handle Pop-up in Selenium WebDriver using Java

Actually, its pretty simple. Use this code ...READ MORE

answered Apr 6, 2018 in Selenium by nsv999
• 5,500 points
10,555 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
13,672 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,142 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
7,221 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,874 views
0 votes
2 answers

How to open a browser window in full screen using Selenium WebDriver with C#

Hi , we have inbuilt method Maximize(). driver.Manage().Wind ...READ MORE

answered Sep 6, 2020 in Selenium by Sri
• 3,190 points
16,031 views
0 votes
2 answers
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP