我试图用一个URL启动chrome浏览器,浏览器启动后,它什么也不做。
1分钟后我看到如下错误:
Unable to open browser with url: 'https://www.google.com' (Root cause: org.openqa.selenium.WebDriverException: unknown error: DevToolsActivePort file doesn't exist
(Driver info: chromedriver=2.39.562718 (9a2698cba08cf5a471a29d30c8b3e12becabb0e9),platform=Windows NT 10.0.15063 x86_64) (WARNING: The server did not provide any stacktrace information)
我的配置:
Chrome浏览器:66
ChromeBrowser: 2.39.56
又及,在Firefox中一切都很好
正如在另一个答案中所述:
此错误消息…这意味着ChromeDriver无法启动/生成一个新的WebBrowser,即Chrome浏览器会话。
Among the possible causes, I would like to mention the fact that, in case you are running an headless Chromium via Xvfb, you might need to export the DISPLAY variable: in my case, I had in place (as recommended) the --disable-dev-shm-usage and --no-sandbox options, everything was running fine, but in a new installation running the latest (at the time of writing) Ubuntu 18.04 this error started to occurr, and the only possible fix was to execute an export DISPLAY=":20" (having previously started Xvfb with Xvfb :20&).
我的问题是php-webdriver中的一个bug。我的代码是:
$chromeOptions = new ChromeOptions();
$chromeOptions->addArguments([
'--headless',
'--no-sandbox',
'--disable-gpu',
'--disable-dev-shm-usage',
'--no-proxy-server'
]);
$chromeOptions->setExperimentalOption('detach', true);
$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(
ChromeOptions::CAPABILITY,
$chromeOptions
);
如你所见,一切都符合其他可能的原因。
但实际上这些功能并没有传递给chromedriver。我不得不改变设置chrome选项功能:
$capabilities->setCapability(
ChromeOptions::CAPABILITY_W3C, // <<< Have to use W3C capabilities with recent versions of Chromedriver.
$chromeOptions->toArray() // <<<<< bug in php-webdriver 1.9, object not getting serialized automatically like with the deprecated capability ChromeOptions::CAPABILITY
);
我的设置是:
$ chromedriver --version
ChromeDriver 79.0.3945.36 (3582db32b33893869b8c1339e8f4d9ed1816f143-refs/branch-heads/3945@{#614})
$ java -jar selenium-server-standalone-3.141.59.jar --version
Selenium server version: 3.141.59, revision: e82be7d358
做了一个bug报告和一个PR来修复php-webdriver中的这个bug
https://github.com/php-webdriver/php-webdriver/issues/849
在我的情况下,这是ubuntu服务器上的CI代理帐户的问题,我用自定义——user-data-dir解决了这个问题
chrome_options.add_argument(“——user-data-dir = ~ / config / google chrome”)
CI代理使用的我的帐户没有必要的权限,有趣的是一切都在根帐户上工作
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--profile-directory=Default')
chrome_options.add_argument('--user-data-dir=~/.config/google-chrome')
driver = webdriver.Chrome(options=chrome_options)
url = 'https://www.google.com'
driver.get(url)
get_url = driver.current_url
print(get_url)