首页 > 解决方案 > 用硒打开的 Chrome 页面保持空白

问题描述

我正在尝试保存网页的屏幕截图,为此我正在尝试使用 Selenium。问题是,一旦打开网页,URL 中的“data:”就会保持空白。

这是我的代码:

from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options,executable_path='aPath/chromedriver.exe',service_log_path='aPath/mylog.txt')
driver.get('http://myURL.html')
screenshot=driver.save_screenshot('aPath/my_screenshot.png')
driver.quit()

注意:我检查了我的 chromedriver 版本是否与我的 chrome 浏览器版本兼容。

标签: pythonseleniumgoogle-chromewebdriver

解决方案


您需要使用chromedriver二进制文件的绝对路径更新Key的,如下所示: executable_pathservice_args

driver = webdriver.Chrome(options=options,executable_path=r'C:\path\to\chromedriver.exe', service_args=["--log-path=C:\\path\\to\\mylog.log"])

您可以在以下位置找到一些相关的讨论:


推荐阅读