Selenium FireFox Driver stops responding after pop-up disappears
<p>I would appreciate some thoughts on a problem at hand that I've been trying to solve for 1-2 days.</p>

<p>I am running a Python script with Selenium 2.53.6 on FireFox 49.0.1. The script is supposed to click a series of document-download links on a page  (I have set the browser to automatically download these file types instead of opening them). Upon clicking, one of the following two events may unfold:</p>

<ol>
<li>A pop-up window appears. A button on the pop-up window needs to be clicked to close it before the document is downloaded.</li>
<li>A blank pop-up appears momentarily before it disappears on its own when the document download begins.</li>
</ol>

<p>Here's an excerpt of the script that is written to handle the events above:</p>

<pre><code>file_link = tr.find_element_by_xpath('td[5]/a')
file_link.click()
time.sleep(7)  # Allows the blank pop-up to disappear automatically under Event2

agree_button = None

# Checks for the pop-up window
try:
    print "Step 1"
    driver.switch_to.window(driver.window_handles[1])
    print "Step 2"  # SCRIPT STOPS RUNNING HERE
    agree_button = driver.find_element_by_xpath('//input[@value="Agree and proceed"]')
    print "Popup found"
except:
    driver.switch_to.window(driver.window_handles[0])

# Clicks the button if the pop-up window is found    
if agree_button is not None:
    agree_button.click()
    print "Button clicked"
</code></pre>

<p>The trouble surfaces when there's high latency in the network for Event 2. Under normal circumstances, the blank pop-up disappears almost instantaneously and the download begins immediately after. However, if the network is slow, the blank pop-up may persist beyond the allocated 7 seconds and that resulted in the script running into "Step 2" before the pop-up window disappears.</p>

<p>Strangely, at this point the script doesn't continue on to look for the <code>agree_button</code>. If it does, then that would have triggered the exception and I would be able to revert back to the original window to resume the steps. The script just stalls and does nothing it seems.</p>

<p>Thanks in advance for your time guys!</p>

<p>You need to keep on waiting until the pop-up disappears. One way you might do this is making below changes in your code: </p>

<pre><code>file_link = tr.find_element_by_xpath('td[5]/a')
file_link.click()
time.sleep(7)  # Allows the blank pop-up to disappear automatically under Event2

agree_button = None

# Checks for the pop-up window
try:
    print "Step 1"
    driver.switch_to.window(driver.window_handles[1])
    print "Step 2"  # SCRIPT STOPS RUNNING HERE
    while True:
        agree_button = driver.find_element_by_xpath('//input[@value="Agree and proceed"]')
        if agree_button is not None:
            break
        else:
            sleep(7)
    print "Popup found"
except:
    driver.switch_to.window(driver.window_handles[0])

# Clicks the button if the pop-up window is found    
if agree_button is not None:
    agree_button.click()
    print "Button clicked"
</code></pre>

python selenium selenium-firefoxdriver