在python中使用Selenium在无头chrome中下载文件时文件未保存

时间:2019-07-01 06:29:45

标签: python python-3.x selenium selenium-webdriver selenium-chromedriver

我能够以普通Chrome模式下载文件。在哪里,我看不到使用硒python在无头chrome中进行的下载。

我希望它不会保存下载的文件

尝试了许多用户在Internet上提供的解决方案,但都不起作用

options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
prefs = {'download.default_directory' :'/Users/nrpss/Downloads'}
options.add_experimental_option('prefs', prefs)

download_path = '/Users/nrpss/Downloads'

browser = webdriver.Chrome('chromedriver.exe', options=options)

browser.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')

params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': download_path}}

command_result = browser.execute("send_command", params)

print ("Headless Chrome Initiated")


### Below is ID for the Download link on webpage 

browser.find_element_by_id('downloadExportLink').click()

time.sleep(50)

def download_completed():
   for i in os.listdir('/Users/nrpss/Downloads'):
       if ".crdownload" in i:
           time.sleep(1)
 download_completed()

预期结果:应下载文件并将其保存在下载文件夹中。

2 个答案:

答案 0 :(得分:0)

尝试添加from bs4 import BeautifulSoup, Tag html = ''' <div class="col-md-6"> <a href="/"><img alt="DC Sustainable Energy Utility: Your Guide to Green" class="img-fluid" src="//d2z33q8cpwfp3p.cloudfront.net/content/dcseu-temp.png"/></a> </div> ''' keyword = 'energy' if __name__ == '__main__': soup = BeautifulSoup(html, 'html.parser') def keyword_filter(el: Tag): """Pick a tag according to its text content""" if keyword.lower() in el.text.lower(): return True try: if keyword.lower() in el['alt'].lower(): return True except KeyError: return False return False for el in soup.find_all(keyword_filter): div = el.find_parent('div') print(div) = download.prompt_for_downloadFalse = download.directory_upgrade,同时将True设置为safebrowsing_for_trusted_sources_enabled以及False

尝试将偏好设置更改为:

safebrowsing.enabled

希望这对您有帮助!

答案 1 :(得分:0)

在Python中启用无头下载:

from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options

options = Options()
options.headless = True 
driver = Chrome(options=options)
params = {'behavior': 'allow', 'downloadPath': '/path/for/download'}
driver.execute_cdp_cmd('Page.setDownloadBehavior', params)