'selenium.common.exceptions.WebDriverException:消息:u'chrome 无法访问

问题描述

我正在使用 webdriver 来配置路由器,但是当我运行脚本时:

I'm using webdriver to config a router, but when I run script:

from selenium import webdriver
self.driver = webdriver.Chrome()

打开chrome没有反应,然后引发异常:

It opens chrome and no response, and then raise exception:

chrome 无法访问.

chrome not reachable.

我的电脑有两张网卡,当我禁止一个时,它工作得很好.
我不知道为什么,请帮忙!

My computer has two network cards, when I forbbiden one, it works well.
I don't konw why, please help!


解决方案

纯情况下chrome not reachable"表示Chrome二进制可以启动但调试端口不可达.

In pure case "chrome not reachable" means that Chrome binary can be started but debugging port is not reachable.

调试端口由参数设置:--remote-debugging-port=12582

Debugging port is set by argument: --remote-debugging-port=12582

就我而言,这是因为沙盒的一些问题:

In my case it happens because some issues with sand-box:

ps afvvx | grep chrome

/opt/google/chrome/chrome --disable-background-networking --disable-client-side-phishing
21026 pts/2    S+     0:00      0    47  6008   100  0.0  |           \_ cat
21027 pts/2    S+     0:00      0    47  6008   100  0.0  |           \_ cat
21029 pts/2    Z+     0:00      0     0     0     0  0.0  |           \_ [chrome-sandbox] <defunct>

当我运行/opt/google/chrome/chrome-sandbox

When I run /opt/google/chrome/chrome-sandbox

# /opt/google/chrome/chrome-sandbox  -h
The setuid sandbox provides API version 1, but you need 0
Please read [https://code.google.com/p/chromium/wiki/LinuxSUIDSandboxDevelopment][1].

close: Bad file descriptor
Read on socketpair: Success

从上面的 url 我无法得到修复 SUID SandBox 的方法,但可以通过 Chrome arg --disable-setuid-sandbox(有时使用 --no-sandbox)将其关闭:

From url above I can't get what I'll to do to fix SUID SandBox, but it can be switched off by Chrome arg --disable-setuid-sandbox(sometimes with --no-sandbox):

import time
from selenium import webdriver

from xvfbwrapper import Xvfb

vdisplay = Xvfb()
vdisplay.start()

from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-setuid-sandbox")

driver = webdriver.Chrome('/usr/local/sbin/chromedriver', chrome_options=chrome_options)  # Optional argument, if not specified will search path.
driver.get('http://www.google.com/xhtml');
time.sleep(5) # Let the user actually see something!
search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(5) # Let the user actually see something!
driver.quit()

vdisplay.stop()

相关文章