Linux 在 chrome 中运行 Selenium WebDriver python 绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8255929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 02:32:51  来源:igfitidea点击:

Running Selenium WebDriver python bindings in chrome

pythonlinuxgoogle-chromeseleniumautomated-tests

提问by Akendo

I ran into a problem while working with Selenium. For my project, I have to use Chrome. However, I can't connect to that browser after launching it with Selenium.

我在使用 Selenium 时遇到了问题。对于我的项目,我必须使用 Chrome。但是,使用 Selenium 启动该浏览器后,我无法连接到该浏览器。

For some reason, Selenium can't find Chrome by itself. This is what happens when I try to launch Chrome without including a path:

出于某种原因,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'

To solve this problem, I then included the Chromium path in the code that launches Chrome. However, the interpreter fails to find a socket to connect to:

为了解决这个问题,我随后在启动 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'

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

回答by adam goucher

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/ChromeDriverfor 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 orthe environment variable. Seems if chromedriver is not in your path you have to pass it in as an argument to the constructor.

是的,似乎是 Python 绑定中的一个错误,从路径环境变量读取 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()

回答by Jachin

Mac OSX only

仅限 Mac OSX

An easier way to get going (assuming you already have homebrewinstalled, 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 放在您的路径中,并且您应该已准备就绪。

回答by user2253967

For windows, please have the chromedriver.exeplaced under <Install Dir>/Python27/Scripts/

对于窗户,请将其chromedriver.exe放在下面<Install Dir>/Python27/Scripts/

回答by Vicky

For Linux

对于 Linux

  1. Check you have installed latest version of chrome brwoser-> chromium-browser -version
  2. If not, install latest version of chrome sudo apt-get install chromium-browser
  3. get appropriate version of chrome driver from here
  4. Unzip the chromedriver.zip
  5. Move the file to /usr/bindirectory sudo mv chromedriver /usr/bin
  6. Goto /usr/bindirectory cd /usr/bin
  7. Now, you would need to run something like sudo chmod a+x chromedriverto mark it executable.
  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()
    display.stop()
    
  1. 检查您是否安装了最新版本的 chrome brwoser-> chromium-browser -version
  2. 如果没有,请安装最新版本的 chrome sudo apt-get install chromium-browser
  3. 这里获取适当版本的 chrome 驱动程序
  4. 解压 chromedriver.zip
  5. 将文件移动到/usr/bin目录sudo mv chromedriver /usr/bin
  6. 转到/usr/bin目录cd /usr/bin
  7. 现在,您需要运行类似sudo chmod a+x chromedriver标记为可执行的程序。
  8. 最后你可以执行代码。

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

回答by Abdul Majeed

For windows

窗户用

Download ChromeDriver from this direct linkORget the latest version from this page.

从此直接链接下载 ChromeDriver从此页面获取最新版本。

Paste the chromedriver.exefile in your C:\Python27\Scriptsfolder.

chromedriver.exe文件粘贴到您的C:\Python27\Scripts文件夹中。

This should work now:

这应该现在工作:

from selenium import webdriver
driver = webdriver.Chrome()

回答by tyrantqiao

For Windows' IDE:

对于 Windows 的 IDE:

If your path doesn't work, you can try to add the chromedriver.exeto your project, like in this project structure.

如果您的路径不起作用,您可以尝试将 加入chromedriver.exe到您的项目中,就像在这个项目结构中一样。

chromedriver.exe

铬驱动程序

Then you should load the chromedriver.exein your main file. As for me, I loaded the driver.exein driver.py.

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

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

..means driver.py'supper directory

..表示driver.py's上层目录

.means the directory where the driver.pyis located

.是指其中的目录driver.py位于

Hope this will be helpful.

希望这会有所帮助。

回答by Ripon Al Wasim

There are 2 ways to run Selenium python tests in Google Chrome. I'm considering Windows (Windows 10 in my case):

有两种方法可以在 Google Chrome 中运行 Selenium python 测试。我正在考虑 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
ii) Set the executable path in your code as below:

i) 将下载的 zip 文件解压缩到您选择的目录/位置
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)
ii) Now write the simple code as below:

i) 只需将 chromedriver.exe 粘贴到 /Python/Scripts/ 下(在我的情况下,文件夹是:C:\Python36\Scripts)
ii) 现在编写如下简单代码:

self.driver = webdriver.Chrome()