1 year ago

#125320

test-img

José Paulo Oliveira Filho

Selenium script to download file on headless mode

This is not a duplicate. I want to achieve this on a headless linux server. No GUI, no browser profile file. I need to set profile preferences in code.

My goal: download an image file generated by a .php + .js script using python selenium firefox on full headless mode.

My issue:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.common.by import By

profile = FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", "/home/ubuntu")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "image/png")

options = Options()
options.headless = True
options.profile = profile

driver = webdriver.Firefox(options=options)
driver.get("https://old.ferramentas.eucreio.org/citacao/")

print(driver.title)

driver.close()

returns:

get_assets.py:6: DeprecationWarning: firefox_profile has been deprecated, please use an Options object
  profile = FirefoxProfile()
get_assets.py:14: DeprecationWarning: Setting a profile has been deprecated. Please use the set_preference and install_addons methods
  options.profile = profile
Traceback (most recent call last):
  File "get_assets.py", line 16, in <module>
    driver = webdriver.Firefox(options=options)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/selenium/webdriver/firefox/webdriver.py", line 179, in __init__
    RemoteWebDriver.__init__(
  File "/home/ubuntu/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 268, in __init__
    self.start_session(capabilities, browser_profile)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 359, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 424, in execute
    self.error_handler.check_response(response)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: Connection refused (os error 111)

If I sneak a options.set_preference() (instead of using the FirefoxProfile instance) in there it doesn't work either. Also trying to set firefox_profile as webdriver.Firefox() parameter has no different outcome.. just says everything is deprecated.

I'm having such a headache with this.. just feel something is so off here and I can't say what.

I've also exhausted the entire internet looking for a code that shed some light on this and.. just nothing.

The internet page is set to download the image as soon as it loads. And all I want is a headless download 😁

Someone with some more experience could help me please? 🙏

python

linux

selenium

selenium-webdriver

firefox-profile

0 Answers

Your Answer

Accepted video resources