Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close the browser after completing a download?

How to make browser closed after completing download?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox()
browser.get(any_url)
browser.find_elements_by_xpath('//input[@value="Download"]').click()

# The program start downloading now.

# HERE WHAT IS THE CODE?

browser.quit()

I want to close the browser only after completing the download.

like image 907
2964502 Avatar asked Jan 19 '14 13:01

2964502


3 Answers

You may want to use the below piece of code right before you close the browser.

time.sleep(5)# Gives time to complete the task before closing the browser. You may increase the seconds to 10 or 15,basically the amount of time required for download otherwise it goes to the next step immediately.

browser.quit()
like image 153
Shashi Shankar Singh Avatar answered Nov 18 '22 13:11

Shashi Shankar Singh


You can use the pause command:

pause ( waitTime )

Wait for the specified amount of time (in milliseconds)

http://release.seleniumhq.org/selenium-core/1.0/reference.html#pause

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox()
browser.get(any_url)
browser.find_elements_by_xpath('//input[@value="Download"]').click()

# The program start downloading now.

pause (10000) # pause/sleeps for 10 seconds

browser.quit()
like image 1
Pedro Lobito Avatar answered Nov 18 '22 14:11

Pedro Lobito


This is an alternative way I did on C#. Maybe you can use the same technique and apply it on python.

public static string GetRequest(string url, bool isBinary = false) {
    // binary is the file that will be downloaded
    // Here you perform asynchronous get request and download the binary
    // Python guide for GetRequest -> http://docs.python-requests.org/en/latest/user/quickstart/
}

browser.webdriver.Firefox();
browser.get(any_url);
elem = browser.findElement("locator");
GetRequest(elem.getAttribute('href'), true); // when this method is done, you expect the get request is done
browser.quit();
like image 1
Major Avatar answered Nov 18 '22 15:11

Major