即使在实现了下面的enable_download_headless(驱动程序,路径)之后,文件的下载也是不正确的。虽然非无头版本总是可以正确下载站点文件,但无头版本下载"chargeinfo.xhtml“节选,这是下载页面"https://www.xxxxx.de/xxx/chargeinfo.xhtml"”链接的最后一个扩展。有趣的是,当我以非头模式调用建议的enable_download_headless(驱动程序,路径)时,它也会下载"chargeinfo.xhtml“。
此外,在点击下载前截图显示的网页布局与非无头页面相同。
任何帮助都是非常感谢的。
这是我的驱动程序设置:
def excerpt():
## declare driver and allow
options = webdriver.ChromeOptions()
##declaring headless
options.add_argument("--headless")
user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.50 Safari/537.36'
options.add_argument(f'user-agent={user_agent}')
options.add_argument('--ignore-certificate-errors')
options.add_argument('--allow-running-insecure-content')
options.add_argument("--window-size=1920,1080")
driver_path = "path/to/chromedriver"
driver = webdriver.Chrome(driver_path,options=options)
####cause the non headless version to also download "chargeinfo.xhtml"
enable_download_headless(driver, "/Download/Path/")
driver.get("https://www.xxxxx.de/xxx/chargeinfo.xhtml")
time.sleep(10)
driver.find_element('xpath', "//span[@class='ui-button-text ui-c' and contains(text(), 'Download')]").click()
def enable_download_headless(browser,download_dir):
browser.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
params = {'cmd':'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': download_dir}}
browser.execute("send_command", params)发布于 2022-09-23 14:55:48
如果有人有类似的问题,对我来说,唯一的方法是切换到请求响应体。我用selenium单击“下载”按钮,然后按以下方式获取响应:
for request in driver.requests:
if request.response:
if request.url == "https://www.xxxxx.de/xxx/chargeinfo.xhtml":
print(
request.url,
request.response.status_code,
request.response.body
)
with open('out.pdf', 'wb') as f:
f.write(request.response.body)https://stackoverflow.com/questions/73804978
复制相似问题