Selenium/Chromedriver hangs on a new tab containing an alert

1,214 views
Skip to first unread message

Phani Vadrevu

unread,
Jun 1, 2017, 8:46:39 PM6/1/17
to webdriver
Hi, 
    I have a minimal example for which Chromedriver seems to hang. Here's the sample HTML pages:

alert.html:

<html>
       
<body>
           
<a href="test_alert.html" target="_blank" id="test">Visit Alert test!</a>
       
</body>
</html>

test_alert.html:

<html>
   
<script type="text/javascript">
        alert
("Hello world");
   
</script>
</html>


Below is the driver Python script for this:

from selenium import webdriver
import time

SHORT_PAUSE
= 5
capabilities
= {'chrome.binary': '/usr/bin/google-chrome'}
driver
= webdriver.Remote('http://localhost:9515', capabilities)
url
= "http://localhost:8888/alert.html"
driver
.get(url)
time
.sleep(SHORT_PAUSE)

element
= driver.find_element_by_id("test")
element
.click()
print "done clicking"

# Usually prints something like: "[u'CDwindow-XXXX', u'CDwindow-YYYY']"
print driver.window_handles
time
.sleep(SHORT_PAUSE)
driver
.switch_to.window(driver.window_handles[1])

# Selenium is unresponsive after this and
# never prints the below line
print "current url:", driver.current_url
alert
= driver.switch_to.alert
print "switched to alert. Text:", alert.text
alert
.accept()
print "Accepted modal dialog...."

Version information:
  • Python: 2.7.6
  • Selenium: 3.4.2
  • Chrome driver: 2.29
  • Chrome: 58.0.3029.110 
  • OS: Ubuntu 14.04.5 LTS an
Please let me know if you need anymore information. I am trying to build a web crawler using Selenium and I have to deal with plenty of such pages. I would appreciate any advice you might have regarding this.

Thanks,
Phani


darrell grainger

unread,
Jun 2, 2017, 9:35:28 AM6/2/17
to webdriver
This is a tough one. When you click the link on the alert.html it will open a new window but then IMMEDIATELY this new window will open an alert. I believe once the alert is open you are losing control in Selenium. You cannot switch to the alert before you open the new window because the alert does not exist. The moment you click the link on the main window, it opens a new window and focus goes to the new alert. I think at that point you need something outside of Selenium to find and click the alert

Essentially, you cannot dismiss or accept the alert until you switch to the new window but you cannot switch to the new window because there is an alert present. Looks like you found something you cannot do with Selenium alone.

thi...@thoughtworks.com

unread,
Jun 5, 2017, 3:06:04 AM6/5/17
to webdriver



The problem is with switching to window..Verify if you are switching to correct window. Verify if driver is passed to this window context...Usually switching to windows take time.Add a sleep for switch to happen and then click on the alert.

SuperKevy

unread,
Jun 5, 2017, 8:32:32 PM6/5/17
to webdriver
The html in test_alert.html is not compliant to the best of my knowledge.
Try the same script within the body. Do you get the same blocking code condition?

<html>
<head></head>
<body>
    <script type="text/javascript">
        alert("Hello world");
    </script>
</body>
</html>

darrell grainger

unread,
Jun 6, 2017, 8:07:40 AM6/6/17
to webdriver
I didn't use Phani's HTML. I had my IDE generate HTML5 compliant versions of the HTML and it still failed. I think the problem is that clicking the link on the first HTML page opens the second page and launches the alert right away. Selenium is still watching the first page. When you navigate to the second page, it does not see the alert because the alert was already present. It feels like Selenium wants to switch to the other page and interact with it somehow but it cannot because there is an alert present.

Darrell

SuperKevy

unread,
Jun 7, 2017, 11:59:58 AM6/7/17
to webdriver
Thanks Darrell +1, 

Do you think async would work on the JS example call?  

Is the page DOM really constructed until the alert is completed?

SuperKevy

unread,
Jun 8, 2017, 12:42:44 PM6/8/17
to webdriver
I should have also mentioned the defer option.

A script that will not run until after the page has loaded:

<script src="demo_defer.js" defer></script>
Reply all
Reply to author
Forward
0 new messages