how to close chrome browser popup dialog using selenium webdriver and python - python

I have a python code that uses selenium webdriver (along with chromedriver), to log into facebook and take a screenshot of the page.
the script itself works as expected, however, after logging in, chrome browser shows a dialog regarding facebook notifications (Chrome Popup)
The dialog seems to be generated by the browser, not the page itself.
i'm unable to identify and capture the element or use "switch to alert" method.
How can i interact with this dialog (either Allow, Dismiss or close it)?
Thanks!

in Python you can use
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--disable-notifications")
webdriver.Chrome(os.path.join(path, 'chromedriver'),
chrome_options=chrome_options)

You can try with launching the chrome browser with disabled pop-ups (browser pop-ups). The following code snippet is in Java. It will be somewhat similar in python i believe.
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-popup-blocking");
options.addArguments("test-type");
ChromeDriver driver = new ChromeDriver(options);

Related

Why driver.get doesn't work in Python Selenium when using Profile

What I need
I need to run Selenium with Chrome Profile
Here is my code:
from selenium import webdriver
import time
options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=/home/atom/.config/google-chrome") #Path to your chrome profile
options.add_argument('--profile-directory=Profile 1')
driver = webdriver.Chrome(options=options)
driver.get('https://youtube.com')
time.sleep(5)
Problem
The code opens Google Chrome instead of Chromedriver. Profile is loaded. But driver.get doesn't work
Question
How can I use Selenium with Chrome profile?
What chromedriver does is briefly described here. When you call get on the webdriver, you are asking for the site to be opened on the chrome browser. If you want to use brave browser or another browser implementation using the chromium engine, you may specify the location of the browser's binary using:
ChromeOptions options = new ChromeOptions();
options.setBinary("/path/to/other/chrome/binary");
You may load a specific profile by using:
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=/path/to/your/custom/profile");
Specifying no profile (not including this option should load the default profile).
A complete list of supported options may be found here.

Why does Chrome load old Gmail Sign-in page in headless mode

I have written a Gmail sign-in script using selenium-python.
When i run in the script in firefox headless mode, then the script works fine, without any issues.
But, when i run the same script in chrome headless mode, then the script fails.
I then added screenshots in my code for debugging.
While running in firefox headless mode, the new Gmail Signin UI is displayed
While running in chrome headless mode, the old Gmail Signin UI is displayed
While in chrome headless mode, selenium fails to find the next button with xpath "//div[#id='identifierNext']", since this xpath is of the next button of the new UI.
However, when i run chrome in non-headless mode, then the new Gmail Signin UI is displayed and the scripts does not fail.
I fail to understand, why the old Gmail Signin UI is displayed in chrome headless mode.
Below is the code, (firefox code is commented since i am having issues in only chrome)
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--headless')
driver = webdriver.Chrome(options=chrome_options, executable_path="C:\\chromedriver.exe")
# from selenium.webdriver.firefox.options import Options
# firefox_options = Options()
# firefox_options.headless = True
# driver = webdriver.Firefox(options=firefox_options,executable_path='C:\\geckodriver.exe')
driver.get("https://www.google.com")
print(driver.title)
driver.find_element_by_xpath("//a[text()='Sign in']").click()
driver.get_screenshot_as_file("screenshot1.png")
driver.find_element_by_xpath("//input[#type='email']").send_keys("abcd")
driver.get_screenshot_as_file("screenshot2.png")
self.wait.until(expected_conditions.visibility_of_element_located((By.XPATH, "//div[#id='identifierNext']"))).click()
If anyone has an explanation for this, kindly let me know.
Thanks.

Accept all cookies automatically in Selenium Chrome driver

I am using Selenium with Chrome web driver in python.
I would like to automatically accept all cookies when opening any website and get rid of the cookie notification like the following:
Is there any option in Chrome driver that allows to do that?
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
options.addArguments("disable-infobars");
please use this

Is there any way to interact with open external application alert from a webpage using selenium?

I tried switching tab that had popup using driver.switch_to.window(driver.window_handles[1])
and
closing the first tab, but none of them worked
This is the code that I tried to accept the popup driver.switch_to_alert().accept()
Have you tried disabling notifications through the chrome options?
This is what I had to use on my webscraping project:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--disable-notifications')
prefs = {'profile.default_content_setting_values.notifications': 2}
chrome_options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=r'C:\Users\jeffg\Desktop\WebScraping\chromedriver.exe')
Documentation can be found here:
https://peter.sh/experiments/chromium-command-line-switches/
https://chromedriver.chromium.org/capabilities
It is not the usual javascript pop up. It is the browser pop up and you can't handle it with the code. I believe that you need to create a script with AutoIT to handle the same.

Selenium Chromedriver - Click Cancel Chrome Authentication Pop Up

I'm using a VPN Chrome app for a python application. Whenever I start up a Chrome instance and Chrome starts to load a website before the VPN addon is ready, the following pop-up appears. As soon as I hit the cancel button, the VPN addon is usually ready and I can access the internet without issues.
I'm looking for a way to click the Cancel button with Selenium.
What I have tried so far:
Set the home page to Chrome Settings -> pop-up doesn't appear because settings are not a website, after a short time.sleep, I can proceed without issues most of the time. Every once in a while, the pop-up still appears.
Using webdriver.ActionChains(driver).send_keys(Keys.ESCAPE).perform()
Whenever I try this, the script freezes, until I hit the Cancel button manually, then the action is executed. The same happens with driver.get(url)and driver.switch_to.alert()
Thanks!
Edit 1 (Setting Chrome options to not show notifications and infobars doesn't resolve the issue):
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import selenium.common.exceptions
options = Options()
options.headless = False
options.add_argument("user-data-dir=ChromeProfiles\Profile_{}".format(22))
options.add_argument("--profile-directory=profile_1")
options.add_argument("--disable-notifications")
options.add_argument("--disable-infobars")
driver = webdriver.Chrome(options=options, executable_path="chromedriver.exe")
As you can see, commands are not executed as long as the popup is open:
1) void dismiss() // To click on the 'Cancel' button of the alert.
driver.switchTo().alert().dismiss();
2) void accept() // To click on the 'OK' button of the alert.
driver.switchTo().alert().accept();
3) String getText() // To capture the alert message.
driver.switchTo().alert().getText();
4) void sendKeys(String stringToSend) // To send some data to alert box.
driver.switchTo().alert().sendKeys("Text");
You can reference all this from Guru 99
Also you should look at which kind of alert it is, browser push notification, browser alert, etc...
These are my chrome options to disable notifications, alerts, and push notifications
chrome.switches = --incognito;--disable-download-notification;--disable-infobars
Also another way to implement chrome options
ChromeOptions ops = new ChromeOptions();
ops.addArguments("--disable-notifications");
ops.addArguments("--disable-infobars");
System.setProperty("webdriver.chrome.driver", "./lib/chromedriver");
driver = new ChromeDriver(ops);
Lastly here are the chrome driver docs to help you further.
ChromeDriver Docs
Edit:
driver.switchTo().activeElement();
driver.close()
Or you can try
Driver.SwitchTo().frame("NameOfFrame");
Driver.findElement("enter path to cancel button").click();
Driver.SwitchTo().defaultContent();
Hope this helps
Either for any Person that is clicking here after years or the original user:
As far as i evaluated it is an simple authentication. So basically you need to send your username and password with it from the start:
https://username:password#domain
domain is url and can be anything. With that the basic authentication is gone and you can basically use it normally.
In Python it would be something along the lines of:
driver.get("https://username:password#domain")
Hope that helps.
At least it solved the problem i had
I also tried many solutions, however the only solution that worked for me was to use the pyautogui package and pressing the escape key to dismiss the popup:
import pyautogui
pyautogui.press('esc')

Categories

Resources