Selenium Chromedriver - Click Cancel Chrome Authentication Pop Up - python

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

Related

Error 401 while using Selenium and Python to log into zoom on Raspberry Pi

I am learning to use Selenium and my goal is to open zoom through a python program on a Raspberry Pi 4. Upon running the pasted code, the program works as intended; opens zoom in browser, maximizes window, selects and clicks sign in, enters credentials and then presses enter. After log in is attempted I am given "error: Http 401 error". I am guessing this is because zoom is detecting an auto-login and blocking me. First off, am I correct? And if so, is there any way to get around this? Or does zoom block any auto filling of credentials.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
driver=webdriver.Chrome()
driver.get("https://zoom.us")
driver.maximize_window()
elem = driver.find_element(By.XPATH, "//a[contains(text(),'SIGN IN')]").click()
emailField = driver.find_element(By.XPATH, "//input[#id='email']")
emailField.send_keys("email") #"email" replaced with zoom login
passField = driver.find_element(By.XPATH, "//input[#id='password']")
passField.send_keys("password") #"password" replaced with zoom password
passField.send_keys(Keys.RETURN)
I faced the same issue, and was able to get around the bot-detection by using
the undetected-chromedriver package.
replace
from selenium import webdriver
...
driver = webdriver.chrome()
with
import undetected_chromedriver.v2 as ucdriver
...
driver = ucdriver.Chrome()
Add this to your code, right after the libraries import, and it will work:
options = webdriver.ChromeOptions()
options.add_argument('--disable-blink-features=AutomationControlled')
#in executable_path put the path to your chromedriver.exe
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://zoom.us")

How to handle Google Authenticator with Selenium

I need to download a massive amount of excel-files (estimated: 500 - 1000) from sellercentral.amazon.de. Manually downloading is not an option, as every download needs several clicks until the excel pops up.
Since amazon cannot provide me a simple xml with its structure, I decided to automate this on my own. The first thing coming to mind was Selenium and Firefox.
The Problem:
A login to sellercentral is required, as well as 2-factor-authentication (2FA). So if I login once, i can open another tab, enter sellercentral.amazon.de and am instantly logged in.
I can even open another instance of the browser, and be instantly logged in there too. They might be using session-cookies. The target URL to "scrape" is https://sellercentral.amazon.de/listing/download?ref=ag_dnldinv_apvu_newapvu .
But when I open the URL from my python-script with selenium webdrive, a new instance of the browser is launched, in which I am not logged in. Even though, there are instances of firefox running at the same time, in which I am logged in. So I guess the instances launched by selenium are somewhat different.
What I've tried:
I tried setting a timedelay after the first .get() (to open site), then I'll manually login, and after that redoing the .get(), which makes the script go on for forever.
from selenium import webdriver
import time
browser = webdriver.Firefox()
# Wait for website to fire onload event
browser.get("https://sellercentral.amazon.de/listing/download?ref=ag_dnldinv_apvu_newapvu")
time.sleep(30000)
browser.get("https://sellercentral.amazon.de/listing/download?ref=ag_dnldinv_apvu_newapvu")
elements = browser.find_elements_by_tag_name("browse-node-component")
print(str(elements))
What am I looking for?
Need solution to use the two factor authentication token from google authenticator.
I want the selenium to be opened up as a tab in the existing instance of the firefox browser, where I will have already logged in beforehand. Therefore no login (should be) required and the "scraping" and downloading can be done.
If there's no direct way, maybe someone comes up with a workaround?
I know selenium cannot download the files itself, as the popups are no longer part of the browser. I'll fix that when I get there.
Important Side-Notes:
Firefox is not a given! I'll gladly accept a solution for any browser.
Here is the code that will read the google authenticator token and used in the login. Used js to open the new tab.
Install pyotp package before running the test code.
pip install pyotp
Test code:
from pyotp import *
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()
driver.get("https://sellercentral.amazon.de/listing/download?ref=ag_dnldinv_apvu_newapvu")
wait = WebDriverWait(driver,10)
# enter the email
email = wait.until(EC.presence_of_element_located((By.XPATH, "//input[#name='email']")))
email.send_keys("email goes here")
# enter password
driver.find_element_by_xpath("//input[#name='password']").send_keys("password goes here")
# click on signin button
driver.find_element_by_xpath("//input[#id='signInSubmit']").click()
#wait for the 2FA feild to display
authField = wait.until(EC.presence_of_element_located((By.XPATH, "xpath goes here")))
# get the token from google authenticator
totp = TOTP("secret goes here")
token = totp.now()
print (token)
# enter the token in the UI
authField.send_keys(token)
# click on the button to complete 2FA
driver.find_element_by_xpath("xpath of the button goes here").click()
# now open new tab
driver.execute_script("""window.open("https://sellercentral.amazon.de/listing/download?ref=ag_dnldinv_apvu_newapvu")""")
# continue with your logic from here

Selenium Ghost browser unexpected exited status 0

Browser opens and driver loses its control. It starts the browser but it can't initiate the driver in order to use it and send_keys, or do anything.
The code runs using Ghost Browser, which is a chromium based browser.
What should be done in order to selenium get control over browser?
Ive tried to get session_id in order to attach selenium to existing browser but it didnt worked also, since it cant get the session_id, because selenium exits.
Code:
exe_path = r'C:\Users\Anonymous\AppData\Local\GhostBrowser\Application\ghost.exe'
driver = webdriver.Chrome(executable_path=exe_path)
driver = webdriver.Chrome(executable_path=exe_path)
Are you sure you use chrome? Maybe change the webdriver.Chrome

how to close chrome browser popup dialog using selenium webdriver and 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);

Does anybody know how to launch the print dialog from chrome by selenium+python

I have a question about how to launch the print dialog from chrome browser. I know the shortcut key to open this is ctrl+p, but I don't know how to describe it in selenium. Does anyone know this? Thanks a lot!
I tried the following code, but it doesn't work on my Chrome browser.
actions = ActionChains(driver)
actions.move_to_element(driver.find_element_by_tag_name('body'))
actions.key_down(Keys.CONTROL).send_keys('T').key_up(Keys.CONTROL).perform()
Basically you need to trigger the JavaScript function that triggers the print pop-up. That function is window.print().
So what you need is to trigger that function after arriving at the page you want to print.
So say you want to print the front page of stackoverflow.com
driver.get("https://stackoverflow.com")
# now you're at the page you want to print. Trigger print function
driver.execute_path("window.print()")
Now it should prompt you to the print pop-up. The reason you need to trigger the JavaScript function is because JavaScript is the browser's language.
Not quite what you are asking about, but this is what worked for me in Firefox.
Send CTRL+P (or COMMAND+P on mac) to the body element using ActionChains:
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://google.com")
actions = ActionChains(driver)
actions.move_to_element(driver.find_element_by_tag_name('body'))
actions.key_down(Keys.CONTROL).send_keys('p').key_up(Keys.CONTROL)
actions.perform()
driver.get("https://stackoverflow.com")
driver.execute_script("window.print()")

Categories

Resources