Python 如何在 Windows 10 上安装 ChromeDriver 并使用 Chrome 运行 Selenium 测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33150351/
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
How do I install ChromeDriver on Windows 10 and run Selenium tests with Chrome?
提问by Uri
We have an Ubuntu server which we use for running Selenium tests with Chrome and Firefox (I installed ChromeDriver) and I also want to run the tests locally on my Windows 10 computer. I want to keep the Python code the same for both computers. But I didn't find out how to install the ChromeDriver on Windows 10? I didn't find it on the documentation[1, 2].
我们有一个 Ubuntu 服务器,用于通过 Chrome 和 Firefox 运行 Selenium 测试(我安装了 ChromeDriver),我还想在我的 Windows 10 计算机上本地运行测试。我想保持两台计算机的 Python 代码相同。但是我没有找到如何在 Windows 10 上安装 ChromeDriver?我没有在文档[1, 2]上找到它。
Here is the code that runs the test in Chrome:
这是在 Chrome 中运行测试的代码:
import unittest
from selenium import webdriver
class BaseSeleniumTestCase(unittest.TestCase):
...
...
...
...
def start_selenium_webdriver(self, chrome_options=None):
...
self.driver = webdriver.Chrome(chrome_options=chrome_options)
...
I also found How to run Selenium WebDriver test cases in Chrome?but it seems to be not in Python (no programming language is tagged, what is it?)
我还发现了如何在 Chrome 中运行 Selenium WebDriver 测试用例?但它似乎不是在 Python 中(没有标记编程语言,它是什么?)
Update #1:I found some Python code in https://sites.google.com/a/chromium.org/chromedriver/getting-started, but where do I put the file in Windows 10 if I want to keep the same Python code for both computers?
更新 #1:我在https://sites.google.com/a/chromium.org/chromedriver/getting-started 中找到了一些 Python 代码,但是如果我想保留相同的 Python,我应该将文件放在 Windows 10 的什么位置两台电脑的代码?
Update #2:I downloaded and put chromedriver.exe
in C:\Windows
and it works, but I didn't see it documented anywhere.
更新2:我下载并把chromedriver.exe
在C:\Windows
和它的作品,但我没有看到它的任何地方记录。
采纳答案by Adam Starrh
As Uri stated in the question, under Update #2, downloading the latest release of chromedriver and placing it in C:\Windows corrects the issue.
正如 Uri 在问题中所述,在Update #2 下,下载最新版本的 chromedriver 并将其放置在 C:\Windows 中可纠正该问题。
I had the same issue with Chrome hanging when the browser window opens (alongside a command prompt window).
当浏览器窗口打开时(与命令提示符窗口一起),Chrome 挂起时我遇到了同样的问题。
The latest drivers can be found at:
可以在以下位置找到最新的驱动程序:
https://sites.google.com/a/chromium.org/chromedriver/downloads
https://sites.google.com/a/chromium.org/chromedriver/downloads
The version in the chromedriver_win32.zip file is working on my 64-bit system.
chromedriver_win32.zip 文件中的版本适用于我的 64 位系统。
回答by Adam Starrh
Let me brief out the requirements first. You need to download the chrome web driver zip from here. https://chromedriver.storage.googleapis.com/index.html?path=2.33/
我先简单介绍一下要求。您需要从此处下载 chrome 网络驱动程序 zip。https://chromedriver.storage.googleapis.com/index.html?path=2.33/
Extract the file and store it in a desired location.
提取文件并将其存储在所需位置。
Create a new project in Eclipse and include the following code in your class.
在 Eclipse 中创建一个新项目并在您的类中包含以下代码。
System.setProperty("webdriver.chrome.driver", "C:\temp\chromedriver.exe");
WebDriver driver = new ChromeDriver();
Explanation : System.setProperty(key,value)
:
说明 : System.setProperty(key,value)
:
Key is default and same for all the systems, value is the location of your chromedriver extract file.
所有系统的键都是默认值,值是 chromedriver 提取文件的位置。
回答by Gaoping
- Download the
chromedriver.exe
and save it to a desired location - Specify the
executable_path
to its saved path
- 下载
chromedriver.exe
并将其保存到所需位置 - 指定
executable_path
到其保存路径
The sample code is below:
示例代码如下:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome(executable_path="path/to/chromedriver.exe", chrome_options=options)
driver.get("example.html")
# do something here...
driver.close()
As Uri stated in Update #2 of the question, if we put the chromedriver.exe
under C:/Windows
, then there is no need to specify executable_path
since Python will search under C:/Windows
.
正如 Uri 在问题的更新 #2 中所述,如果我们将 放在chromedriver.exe
下C:/Windows
,则无需指定,executable_path
因为 Python 将在 下搜索C:/Windows
。