Skip to content
Advertisement

How to deal with google chrome confirmation alerts when using selenium in python?

I am trying to make a web automation program that opens my school’s app. Our school is using Adobe Connect. When I try to open the installed app, it gives me a confirmation alert. Which is not inspectable and can’t define any XPath or… So, how can I handle this? I have read many QAs but I can’t manage it. Here is my code:

from selenium import webdriver
import time

url = "http://78.157.45.27/eleven203/?OWASP_CSRFTOKEN=edd648edd5" 
      "e9bf51c3196210963bd5470d6c7af82e0ec636789dfafe5fc558dc&proto=true"
browser = webdriver.Chrome()
page = browser.get(url)
time.sleep(1)

my_user_name = "***"
my_password = "***"
time.sleep(1)

user_name = browser.find_element_by_id(id_="name")
password = browser.find_element_by_id(id_="pwd")
submit_button = browser.find_element_by_id(id_="login-button")
time.sleep(1)

user_name.click()
user_name.clear()
user_name.send_keys(my_user_name)

password.click()
password.clear()
password.send_keys(my_password)

submit_button.click()
time.sleep(5)

And here is the solution I tested from other questions but not worked(attach it at the end of the code above):

ale = browser.switch_to.alert()
ale.accept()

The error message:

selenium.common.exceptions.NoAlertPresentException: Message: no such alert

I also added the confirmation alert, for your ease.

enter image description here

Advertisement

Answer

I’m not sure if you can set apps to automatically open in the browser prefs or not, but you could always take a cropped screeshot of the button “Open Adobe Connect” and then use pyautogui to locate it and click it:

import pyautogui
button = pyautogui.locateCenterOnScreen('croppedScreenshot.png')
pyautogui.moveTo(button)
pyautogui.click()

Pyautogui sendkeys could work too I’d imagine.

Advertisement