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,280 points
2,843 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,510 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
0 votes
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,430 points
2,442 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 in Selenium by Abha
• 27,200 points
347 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 5, 2018 in Selenium by nsv999
• 5,430 points
5,326 views
0 votes
1 answer

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,430 points
1,193 views
0 votes
1 answer
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,510 points
1,277 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,510 points
1,036 views
0 votes
1 answer

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

Simply Use this: driver.Manage().Window.Maximize(); READ MORE

answered Jun 7, 2018 in Selenium by Samarpit
• 5,510 points
4,646 views
0 votes
2 answers

How to get HTML Source of WebElement in Selenium WebDriver using Python?

You can read innerHTML attribute to get source of ...READ MORE

answered Jun 13, 2018 in Selenium by Samarpit
• 5,510 points
19,376 views