我已经设置了具有多种配置的webdriver,但仍然无法仅以无头模式下载文件。
我尝试添加POST命令
chrome_options = webdriver.ChromeOptions()
prefs = {
"download.default_directory": path,
# "directory_upgrade": True,
# "safebrowsing.enabled": False,
"safebrowsing.disable_download_protection": True}
chrome_options.add_experimental_option("prefs", prefs)
if headless==True:
chrome_options.add_argument('--headless')
# chrome_options.add_argument('log-level=2')
# chrome_options.add_argument('--disable-extensions')
# chrome_options.add_argument('--disable-gpu')
# chrome_options.add_argument('--no-sandbox')
local="/home/rittle/Workspace/portal_dl/chromedriver"
EC2="/home/ubuntu/portal_dl/chromedriver"
browser = webdriver.Chrome(executable_path=local,
chrome_options=chrome_options,
)
browser.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': path}}
command_result = browser.execute("send_command", params)
我希望一旦单击页面上的一个按钮后就会有一个.crdownload文件,但是我的/ tmp /目录为空,此代码可以正常运行
答案 0 :(得分:0)
首先,请参见此bug thread。
截至4天前...您需要的是增加无头的大小...
if headless==True:
chrome_options.add_argument('--headless')
chrome_options.add_argument('--window-size=1920,1080')
根据错误:
未知错误:DevToolsActivePort文件不存在
您可以添加--disable-dev-shm-usage
作为参数:
chrome_options.add_argument('--disable-dev-shm-usage')
希望这对您有帮助!
答案 1 :(得分:0)
使用execute命令覆盖BrowserManagmentKeywords(在SeleniumLibrary / keywords / browsermanagement.py下)的create_webdriver方法,以启用无头chrome浏览器下载。
from SeleniumLibrary.base import keyword
from SeleniumLibrary.keywords import BrowserManagementKeywords
from selenium import webdriver
class CustomKeywords():
def __init__(self):
self.logger('Initializing Custom Keywords')
@keyword
def create_webdriver(self, driver_name, alias=None, kwargs={},
**init_kwargs):
if not isinstance(kwargs, dict):
raise RuntimeError("kwargs must be a dictionary.")
for arg_name in kwargs:
if arg_name in init_kwargs:
raise RuntimeError("Got multiple values for argument '%s'." % arg_name)
init_kwargs[arg_name] = kwargs[arg_name]
driver_name = driver_name.strip()
try:
creation_func = getattr(webdriver, driver_name)
except AttributeError:
raise RuntimeError("'%s' is not a valid WebDriver name." % driver_name)
driver = creation_func(**init_kwargs)
options = init_kwargs['chrome_options']
args = options.to_capabilities()[options.KEY]['args']
is_headless = '--headless' in args or 'headless' in args
if is_headless:
prefs = options.to_capabilities()[options.KEY]['prefs']
download_dir = prefs['download.default_directory']
self.enable_download_in_headless_chrome(driver, download_dir)
driver = BrowserManagementKeywords(self)._wrap_event_firing_webdriver(driver)
return BrowserManagementKeywords(self).ctx.register_driver(driver, alias)
@keyword
def enable_download_in_headless_chrome(self, driver, download_dir):
#commands to enable download feature in headless chrome browser
driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': download_dir}}
driver.execute("send_command", params)