Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Selenium Chrome选项和功能_Python_Selenium_Google Chrome_Selenium Chromedriver_Desiredcapabilities - Fatal编程技术网

Python Selenium Chrome选项和功能

Python Selenium Chrome选项和功能,python,selenium,google-chrome,selenium-chromedriver,desiredcapabilities,Python,Selenium,Google Chrome,Selenium Chromedriver,Desiredcapabilities,我正在尝试使用selenium自动下载一个文件。为此,我想设置默认下载目录并禁用下载提示。它似乎不起作用,我通过的选项甚至似乎没有注册。下面是我如何创建浏览器的示例。有人知道发生了什么事吗 chromedriver = 'PATH/TO/chromedriver' download_fp = './testPrismaDownload/' prefs = { "download.prompt_for_download" : False, "download.default_di

我正在尝试使用selenium自动下载一个文件。为此,我想设置默认下载目录并禁用下载提示。它似乎不起作用,我通过的选项甚至似乎没有注册。下面是我如何创建浏览器的示例。有人知道发生了什么事吗

chromedriver = 'PATH/TO/chromedriver'

download_fp = './testPrismaDownload/'
prefs = {
    "download.prompt_for_download" : False,
    "download.default_directory": download_fp
}

options = webdriver.ChromeOptions()
options.binary_location = '/usr/bin/google-chrome-stable'
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_argument('--disable-setuid-sandbox')
options.add_experimental_option('prefs', prefs)

# i've tried various combinations of `options`, `chrome_options` (deprecated) and `desired_capabilities`
browser = webdriver.Chrome(options=options, desired_capabilities=options.to_capabilities(), executable_path=chromedriver)
我指定的选项均未显示在
浏览器中。功能
浏览器中。所需的功能
。例如,功能中chromeOptions的键是
goog:chromeOptions':{'debuggerAddress':'localhost:42911'}

当我执行
download\u按钮时。单击()
命令将成功,但不会下载任何内容。我也在我的mac笔记本电脑上试用过,没有--headless选项,当我点击下载按钮时,浏览器会打开下载对话框,提示下载确认

如有任何帮助/经验,将不胜感激

Python 3.6.6::Anaconda公司

硒'3.141.0'

Linux 9725a3ce7b7e 4.9.125-linuxkit#1 SMP周五9月7日08:20:28 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux出现了一个问题:

这很容易。在驾驶员的切换窗口后调用此启用码功能:


正在获取self.driver.switch_to.window(实例[1])索引器:列表索引超出范围。
 def enable_download_in_headless_chrome(driver, download_dir):
    # add missing support for chrome "send_command"  to selenium webdriver
    driver.command_executor._commands["send_command"] = ("POST",'/session/$sessionId/chromium/send_command')
    params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': download_dir}}
    command_result = driver.execute("send_command", params)

expected_download = 'ur/download/path'
opt = Options()
opt.add_experimental_option("prefs", { \
    'download.default_directory': expected_download,
     'download.prompt_for_download': False,
     'download.directory_upgrade': True,
  })
opt.add_argument('--headless')
opt.add_argument('--window-size=1920,1080');

login_page = "https://www.google.com"
driver = webdriver.Chrome(options=opt)
driver.implicitly_wait(5)
driver.get(login_page)
driver.maximize_window()

#On below click you will be in new tab
scoresheet_tab = driver.find_element_by_xpath("//*[@class='sideNav-item is--scoresheet']").click()

instances = driver.window_handles
driver.switch_to.window(instances[1]) # this is the new browser
#this below function below does all the trick
enable_download_in_headless_chrome(driver, expected_download)