2

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.

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

    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();

Kindly suggest how to download the file using these 3 steps. Thanks

Aditya
  • 615
  • 3
  • 12
  • 26
  • Does this have to be done in selenium? When it is just about reading download-links and downloading certain files the lib requests should do fine. When it is about testing download-links you are right in selenium. – jhinghaus Aug 30 '16 at 07:41

2 Answers2

0

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

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

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)

Reference : https://sites.google.com/a/chromium.org/chromedriver/capabilities

Satish Gupta
  • 1,447
  • 1
  • 12
  • 14
  • For what it's worth, you can use [selenium](https://stackoverflow.com/questions/52394408/how-to-use-chrome-profile-in-selenium-webdriver-python-3) and keep your Chrome profile signed in through that Chrome instance. So if you just choose to "Always Download These Files" then it won't open the system dialog box. – Hercislife Feb 15 '21 at 15:24
-1

As per java you can use following code for clicking save as link and click save in new window

try {
    Robot bot = new Robot();
    //location of link on your page
    bot.mouseMove(5, 12);    
    //instigate right click of mouse
    bot.mousePress(InputEvent.BUTTON1_MASK);
    bot.mouseRelease(InputEvent.BUTTON1_MASK);
    Thread.sleep(1000); 
    /* repeatedly press down arrow until you reach save as link in context menu */
    bot.keyPress(KeyEvent.VK_DOWN);
    Thread.sleep(1000);
    bot.keyPress(KeyEvent.VK_DOWN);
    Thread.sleep(1000);
    bot.keyPress(KeyEvent.VK_DOWN);
    Thread.sleep(1000);
    bot.keyPress(KeyEvent.VK_DOWN);
    //press enter to select save as link
    Thread.sleep(1000);
    //press enter to press save button on new window
    bot.keyPress(KeyEvent.VK_ENTER);                
    Thread.sleep(1000);
    bot.keyPress(KeyEvent.VK_ENTER);
} catch (Exception e) {
    e.printStackTrace();
}

As per your question you need python code so you can convert this code in python by using this link https://pypi.python.org/pypi/py_robot/0.1

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Yash Gupta
  • 24
  • 3