0

这不是重复的。我想在无头 linux 服务器上实现这一点。没有 GUI,没有浏览器配置文件。我需要在代码中设置配置文件首选项。

我的目标:在完全无头模式下使用 python selenium firefox 下载由 .php + .js 脚本生成的图像文件。

我的问题:

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

返回:

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)

如果我在那里偷偷摸摸options.set_preference()(而不是使用FirefoxProfile实例)它也不起作用。也尝试设置firefox_profilewebdriver.Firefox()参数没有不同的结果..只是说一切都已弃用。

我对此感到非常头疼..只是觉得这里有些不对劲,我说不出是什么。

我还用尽了整个互联网来寻找可以对此有所了解的代码,并且……什么都没有。

互联网页面设置为在加载后立即下载图像。我想要的只是无头下载

请有更多经验的人可以帮助我吗?

4

0 回答 0