Selenium没有启动便携式镀铬但是本地安装

时间:2015-09-28 07:44:03

标签: google-chrome selenium

我遇到了Selenium网络驱动程序的问题。我要做的是启动“便携式”chrome而不是本地安装,因为它有不同的设置。

问题是便携式Chrome(来自PortableApps)似乎只在使用GoogleChromePortable.exe时启动。如果我直接使用Chrome二进制文件,它将启动我的本地安装。 使用Selenium似乎无论我传递给它的Chrome路径(GoogleChromePortable.exe或二进制路径),它都会启动我的本地安装。

这是我的代码:

String chromePath = "M:/my/path";
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
capabilities.setCapability("chrome.binary", chromePath);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);

有关如何启动便携式镀铬的任何想法? 感谢

4 个答案:

答案 0 :(得分:3)

对于遇到此问题的其他人来说,以下是我设法让便携式Chrome启动的方法:

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary(binaryPath);
driver = new ChromeDriver(chromeOptions);

答案 1 :(得分:1)

  String chromePath = "M:/my/googlechromeporatble.exe path"; 
  String chromedriverpath="M:/my/chromedriver.exe path";
  ChromeOptions options = new ChromeOptions();
  options.setBinary(chromepath);
  System.setProperty("webdriver.chrome.driver",chromedriverpath);              
  driver = new ChromeDriver(options);

这将调用便携式chrome而不是本地安装。 首先设置google chrome portable path然后调用chromeriver.exe

答案 2 :(得分:1)

我在Windows 10上使用Python 3.7,并从PortableApps.com获得了Chrome Portable。

@ mario.schlipf和@SeJaPy的

评论很有帮助,但我注意到在较新的Webdriver版本中, setbinary 方法已由 binary_location

这实际上对我有用:

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

chromedriverpath='M:/my/chromedriver.exe'
chromePath = 'M:/my/App/Chrome-bin/chrome.exe'    #  <==  IMPORTANT! See note below.

chromeoptions = Options()
chromeoptions.add_argument('--incognito')
chromeoptions.binary_location = chromePath   

browser = webdriver.Chrome(executable_path=chromedriverpath, options=chromeoptions)

注意:

chromePath 变量必须指向可移植环境中的Chrome可执行文件。

在从PortableApps.com获得的软件包中,您有两个可执行文件:安装目录(实际上是解压缩文件)中的 GoogleChromePortable.exe 和<<中的 chrome.exe em> [installdirectory] ​​/ App / Chrome-bin ,第一个只是“启动器”,为可移植的应用程序提供一致的环境。

正如我所观察到的,chromedriver需要直接与“真实的” Chrome可执行文件进行交互,否则脚本将启动浏览器(通过启动器),但最终会崩溃并显示错误消息:

unknown error: DevTools Active Port file doesn't exist

,因此不会返回任何浏览器会话。

对于许多人来说,这似乎很明显……但是对我而言却不是,所以我决定放这条笔记,以使那些不太聪明的人(包括我在内)更加清晰:

答案 3 :(得分:0)

根据您在ChromePortable中的设置,也许您可​​以使用Capabilities & ChromeOptions默认ChromeDriver?

我特别想custom profile。如果你以某种方式从ChromePortable获得并使用默认的ChromeDriver加载它?

编辑:也许this可以提供帮助

相关问题