简体   繁体   English

在 chrome 中运行 Selenium WebDriver python 绑定

[英]Running Selenium WebDriver python bindings in chrome

I ran into a problem while working with Selenium.我在使用 Selenium 时遇到了问题。 For my project, I have to use Chrome.对于我的项目,我必须使用 Chrome。 However, I can't connect to that browser after launching it with Selenium.但是,使用 Selenium 启动该浏览器后,我无法连接到该浏览器。

For some reason, Selenium can't find Chrome by itself.出于某种原因,Selenium 无法自己找到 Chrome。 This is what happens when I try to launch Chrome without including a path:当我尝试在不包含路径的情况下启动 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'

To solve this problem, I then included the Chromium path in the code that launches Chrome.为了解决这个问题,我随后在启动 Chrome 的代码中包含了 Chromium 路径。 However, the interpreter fails to find a socket to connect to:但是,解释器无法找到要连接的套接字:

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'

I also tried solving the problem by launching chrome with:我还尝试通过启动 chrome 来解决这个问题:

chromium --remote-shell-port=9222

However, this did not work either.然而,这也不起作用。

PS.附注。 Here's some information about my system:以下是有关我的系统的一些信息:

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

You need to make sure the standalone ChromeDriver binary (which is different than the Chrome browser binary) is either in your path or available in the webdriver.chrome.driver environment variable.您需要确保独立的 ChromeDriver 二进制文件(与 Chrome 浏览器二进制文件不同)在您的路径中或在 webdriver.chrome.driver 环境变量中可用。

see http://code.google.com/p/selenium/wiki/ChromeDriver for full information on how wire things up.有关如何连接的完整信息,请参阅http://code.google.com/p/selenium/wiki/ChromeDriver

Edit:编辑:

Right, seems to be a bug in the Python bindings wrt reading the chromedriver binary from the path or the environment variable.是的,似乎是 Python 绑定中的一个错误,即从路径环境变量中读取 chromedriver 二进制文件。 Seems if chromedriver is not in your path you have to pass it in as an argument to the constructor.似乎如果 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()

For Linux对于 Linux

  1. Check you have installed latest version of chrome brwoser-> chromium-browser -version检查您是否安装了最新版本的 chrome brwoser-> chromium-browser -version

  2. If not, install latest version of chrome sudo apt-get install chromium-browser如果没有,请安装最新版本的 chrome sudo apt-get install chromium-browser

  3. get appropriate version of chrome driver from here这里获取适当版本的 chrome 驱动程序

  4. Unzip the chromedriver.zip解压 chromedriver.zip

  5. Move the file to /usr/bin directory sudo mv chromedriver /usr/bin将文件移动到/usr/bin目录sudo mv chromedriver /usr/bin

  6. Goto /usr/bin directory cd /usr/bin转到/usr/bin目录cd /usr/bin

  7. Now, you would need to run something like sudo chmod a+x chromedriver to mark it executable.现在,您需要运行诸如sudo chmod a+x chromedriver来将其标记为可执行。

  8. finally you can execute the code.最后你可以执行代码。

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

Mac OSX only仅限 Mac OSX

An easier way to get going (assuming you already have homebrew installed, which you should, if not, go do that first and let homebrew make your life better) is to just run the following command:一个更简单的开始方法(假设你已经安装了自制软件,如果没有,你应该先这样做,让自制软件让你的生活更美好)是运行以下命令:

brew install chromedriver

That should put the chromedriver in your path and you should be all set.这应该将 chromedriver 放在您的路径中,并且您应该已准备就绪。

For windows窗户用

Download ChromeDriver from this direct link OR get the latest version from this page .从此直接链接下载 ChromeDriver从此 页面获取最新版本。

Paste the chromedriver.exe file in your C:\\Python27\\Scripts folder.chromedriver.exe文件粘贴到C:\\Python27\\Scripts文件夹中。

This should work now:这应该现在工作:

from selenium import webdriver
driver = webdriver.Chrome()

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

There are 2 ways to run Selenium python tests in Google Chrome.有两种方法可以在 Google Chrome 中运行 Selenium python 测试。 I'm considering Windows (Windows 10 in my case):我正在考虑 Windows(在我的情况下是 Windows 10):

Prerequisite: Download the latest Chrome Driver from: https://sites.google.com/a/chromium.org/chromedriver/downloads先决条件:从以下位置下载最新的 Chrome 驱动程序: https : //sites.google.com/a/chromium.org/chromedriver/downloads

Way 1:方式一:

i) Extract the downloaded zip file in a directory/location of your choice i) 将下载的 zip 文件解压缩到您选择的目录/位置
ii) Set the executable path in your code as below: ii) 在您的代码中设置可执行路径,如下所示:

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

Way 2:方式二:

i) Simply paste the chromedriver.exe under /Python/Scripts/ (In my case the folder was: C:\\Python36\\Scripts) i) 只需将 chromedriver.exe 粘贴到 /Python/Scripts/ 下(在我的情况下,文件夹是:C:\\Python36\\Scripts)
ii) Now write the simple code as below: ii) 现在编写简单的代码如下:

self.driver = webdriver.Chrome()

For Windows' IDE:对于 Windows 的 IDE:

If your path doesn't work, you can try to add the chromedriver.exe to your project, like in this project structure.如果您的路径不起作用,您可以尝试将chromedriver.exe添加到您的项目中,就像在此项目结构中一样。

铬驱动程序

Then you should load the chromedriver.exe in your main file.然后你应该在你的主文件中加载chromedriver.exe As for me, I loaded the driver.exe in driver.py .至于我,我在driver.exe中加载了driver.py

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

.. means driver.py's upper directory ..表示driver.py's上层目录

. means the directory where the driver.py is located表示driver.py所在的目录

Hope this will be helpful.希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM