在Chrome中运行Selenium WebDriver Python绑定

falq053o  于 2023-02-27  发布在  Go
关注(0)|答案(8)|浏览(194)

我在使用Selenium时遇到了一个问题。对于我的项目,我必须使用Chrome。但是,我在使用Selenium启动后无法连接到该浏览器。
由于某些原因,Selenium无法自己找到Chrome,这就是我在没有路径的情况下启动Chrome时发生的情况:

Traceback (most recent call last):
  File "./obp_pb_get_csv.py", line 73, in <module>
    browser = webdriver.Chrome() # Get local session of chrome
  File "/usr/lib64/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 46, in __init__
    self.service.start()
  File "/usr/lib64/python2.7/site-packages/selenium/webdriver/chrome/service.py", line 58, in start
    and read up at http://code.google.com/p/selenium/wiki/ChromeDriver")
selenium.common.exceptions.WebDriverException: Message: 'ChromeDriver executable needs to be available in the path.                 Please download from http://code.google.com/p/selenium/downloads/list                and read up at http://code.google.com/p/selenium/wiki/ChromeDriver'

为了解决这个问题,我在启动Chrome的代码中加入了Chromium路径,但是解释器找不到要连接的套接字:

Traceback (most recent call last):
  File "./obp_pb_get_csv.py", line 73, in <module>
    browser = webdriver.Chrome('/usr/bin/chromium') # Get local session of chrome
  File "/usr/lib64/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 46, in __init__
    self.service.start()
  File "/usr/lib64/python2.7/site-packages/selenium/webdriver/chrome/service.py", line 64, in start
    raise WebDriverException("Can not connect to the ChromeDriver")
selenium.common.exceptions.WebDriverException: Message: 'Can not connect to the ChromeDriver'

我也尝试过用以下命令启动chrome来解决这个问题:

chromium --remote-shell-port=9222

然而,这也不起作用。
附言。以下是关于我的系统的一些信息:

www-client: chromium 15.0.874.121
dev-lang:   python 2.7.2-r3 Selenium 2.11.1
OS:         GNU/Linux Gentoo Kernel 3.1.0-gentoo-r1
qf9go6mv

qf9go6mv1#

您需要确保独立的ChromeDriver二进制文件(与Chrome浏览器二进制文件不同)在您的路径中或在webdriver. chrome. driver环境变量中可用。
有关如何连接的完整信息,请参见http://code.google.com/p/selenium/wiki/ChromeDriver
编辑:
对,看起来是Python绑定中的一个bug,wrt从路径 * 或 * 环境变量读取chromedriver二进制文件。看起来如果chromedriver不在你的路径中,你必须把它作为一个参数传递给构造函数。

import os
from selenium import webdriver

chromedriver = "/Users/adam/Downloads/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
driver.get("http://stackoverflow.com")
driver.quit()
tnkciper

tnkciper2#

    • 适用于Linux**

1.检查您是否安装了最新版本的chrome brwoser-〉chromium-browser -version
1.如果没有,请安装最新版本的chrome sudo apt-get install chromium-browser
1.从here获取适当版本的chrome驱动程序
1.解压缩www.example.com chromedriver.zip
1.将文件移动到/usr/bin目录sudo mv chromedriver /usr/bin
1.转到/usr/bin目录cd /usr/bin
1.现在,您需要运行类似sudo chmod a+x chromedriver这样的代码来将其标记为可执行。
1.最后你可以执行代码。

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("http://www.google.com")
print driver.page_source.encode('utf-8')
driver.quit()
gj3fmq9x

gj3fmq9x3#

仅适用于Mac OS X

一个更简单的方法是运行下面的命令(假设您已经安装了Homebrew,如果没有,您应该先安装,让Homebrew让您的生活变得更好):

brew install chromedriver

这应该会把chromedriver放在你的路径上,你应该已经准备好了。

v8wbuo2f

v8wbuo2f4#

适用于Windows

this direct link下载ChromeDriver***或***从this page获取最新版本。
chromedriver.exe文件粘贴到C:\Python27\Scripts文件夹中。
现在应该可以了:

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

sqyvllje5#

对于Windows,请将chromedriver.exe放在<Install Dir>/Python27/Scripts/下。

4dc9hkyq

4dc9hkyq6#

在Ubuntu上,你可以这样做:

sudo apt install chromium-chromedriver

应该会有用的。

5rgfhyps

5rgfhyps7#

在Google Chrome中运行Selenium Python测试有两种方法。我考虑使用Windows(我的例子是Windows 10):

    • 先决条件:*从以下网址下载最新的Chrome驱动程序: 一个月一次 *
      方式1:

i)将下载的ZIP文件解压到您选择的目录/位置
ii)在代码中设置可执行路径,如下所示:

self.driver = webdriver.Chrome(executable_path='D:\Selenium_RiponAlWasim\Drivers\chromedriver_win32\chromedriver.exe')

方式2:

i)只需将 * chromedriver. exe * 粘贴到 〈安装目录〉/Python/Scripts/下(在我的例子中,文件夹是: C:\Python 36\脚本)
ii)现在编写如下简单代码:

self.driver = webdriver.Chrome()
0mkxixxg

0mkxixxg8#

对于Windows的IDE:
如果您的路径不起作用,您可以尝试将chromedriver.exe添加到您的项目中,就像下面的项目结构一样。

然后你应该在你的主文件中加载chromedriver.exe。至于我,我在driver.py中加载了driver.exe

def get_chrome_driver():
return webdriver.Chrome("..\\content\\engine\\chromedriver.exe",
                            chrome_options='--no-startup-window')

..表示driver.py's上层目录
.表示driver.py所在的目录

相关问题