Python - Select "Save link as" and save a file using Selenium
<p>Newbie: There are different files on a webpage, which can be downloaded as follows:
1. Right click on a file link
2. Select "Save link as"
3. Click "Save" button on the new window.</p>

<p>I tried the following code(for first 2 steps), but it is not working:</p>

<pre><code>    from selenium import webdriver
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.keys import Keys
    import time

    driver = webdriver.Chrome()
    driver.maximize_window() 

    driver.get('www.example.com')
    time.sleep(1)

    driver.find_element_by_link_text("MarketFiles/").click()


    actionChains = ActionChains(driver)

    download_file = "Market_File1.csv"
    link = driver.find_element_by_link_text(download_file)

    actionChains.context_click(link).send_keys(Keys.ARROW_DOWN).send_keys(Keys.ARROW_DOWN).send_keys(Keys.ARROW_DOWN).send_keys(Keys.ARROW_DOWN).send_keys(Keys.RETURN).perform();
</code></pre>

<p>Kindly suggest how to download the file using these 3 steps. Thanks</p>

<p>Save Link As will open the system dialog which can not be controlled through selenium directly.</p>

<p>Having said that, download preferences can be configured in the profile, which can be used while launching the browser and in that case, any click to download will save the file as per preferences in the chrome profile</p>

<pre><code>chromeOptions = webdriver.ChromeOptions()
prefs = {"download.default_directory" : "/some/path"}
chromeOptions.add_experimental_option("prefs",prefs)
chromedriver = "path/to/chromedriver.exe"
driver = webdriver.Chrome(executable_path=chromedriver, chrome_options=chromeOptions)
</code></pre>

<p>Reference : <a href="https://sites.google.com/a/chromium.org/chromedriver/capabilities" rel="nofollow">https://sites.google.com/a/chromium.org/chromedriver/capabilities</a></p>

python selenium