I'm writing a Python (3.6.6) script that uses Selenium (3.141.0) to open a URL in Chrome (ChromeDriver version 77.0.3865.40), navigate a series of menus, enter login details into a popup and login. Here's my script:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
url = 'https://some.url.com/'
driver = webdriver.Chrome()
action = ActionChains(driver)
driver.get(url)
button_1 = driver.find_element_by_xpath("//*[contains(text(), 'button_1_text')]")
button_1.click()
button_2 = driver.find_element_by_id('button_2_text')
button_2 .click()
link = driver.find_element_by_link_text('link_text')
action.move_to_element(link).perform()
link.click()
alert_obj = driver.switch_to.alert
If I perform this navigation manually, I get a prompt asking for a username and password, with two buttons to sign in or cancel. When Selenium does it, I see the prompt flash up, but then quickly disappear, so I get the error:
selenium.common.exceptions.NoAlertPresentException: Message: no such alert
If I refresh the still open browser manually I can get the alert to appear, but if I script a refresh the prompt once again vanishes. I've tried putting a wait in for an alert to appear, as described in Check if any alert exists using selenium with python, but the wait just times out.
If I check the source code of the page that is left open once the alert has disappeared, I see this:
<!-- template name: form.autopost.template.html -->
<html>
<head>
<title>Submit Form</title>
</head>
<body onload="javascript:document.forms[0].submit()">
<noscript>
<p>
<strong>Note:</strong> Since your browser does not support JavaScript, you must press the Resume button once to proceed.
</p>
</noscript>
<form method="post" action="another_url.saml2">
<input type="hidden" name="SAMLRequest" value="really_long_token"/>
<input type="hidden" name="RelayState" value="token"/>
<noscript><input type="submit" value="Resume"/></noscript>
</form>
</body>
</html>
So I tried explicitly enabling JavaScript with:
options = webdriver.ChromeOptions()
options.add_argument("--enable-javascript")
driver = webdriver.Chrome(chrome_options=options)
This seems similar to Selenium test - Firefox alert disappearing immediately, but it looks like I can't recreate the solution using Chrome. I tried the following:
options = webdriver.ChromeOptions()
options.add_argument("--disable-popup-blocking")
driver = webdriver.Chrome(chrome_options=options)
But that made no difference either. Is there another way to prevent Selenium/Chrome from automatically closing the alert?
Here is a screenshot of the popup:
I have tried adding the following to the end of my script:
current_url = driver.current_url
parts = current_url.split('//')
login_url = parts[0] + '//{}:{}@'.format(username, password) + parts[1]
driver.get(login_url)
But this doesn't help. The login URL looks like this: