0

Trying to extract an image, successfully triggered "Save Image as..." dialog, but couldn't send any keys, is there a way to solve this problem?

driver = webdriver.Firefox()
actions = webdriver.ActionChains(driver)
actions.move_to_element(img).context_click(img).send_keys('v').perform()

time.sleep(2)

# and this line does not work
actions.send_keys('image.jpg').perform()

Only one step away from making everything works, what should I do?

Shane
  • 4,875
  • 12
  • 49
  • 87
  • 1
    Selenium is mostly used as a tool for automated browser website **testing**. It seems weird to include saving an image as part of a website test, since that doesn't do anything for the site. If your actual goal is to download images from websites, you don't need to remote control a browser for this. You just need to download the HTML, find the image, then download and save that URL. Can be done in roughly the same amount of code as remote controlling a browser using selenium. – deceze Mar 13 '15 at 14:10

1 Answers1

1

This is a kind of popup you cannot control with selenium.

In this case you need to ask the browser to save the file automatically by tweaking it's preferences (aka desired capabilities):

profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", "/path/to/file")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "image/jpeg")

driver = webdriver.Firefox(firefox_profile=profile)

where browser.helperApps.neverAsk.saveToDisk setting value should have a mime-type (or a comma-separated list of mime-types) of the files that should be downloaded automatically.

See also:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195