如何在Windows 10上安装ChromeDriver并使用Chrome运行Selenium测试?

Uri*_*Uri 16 python selenium webdriver selenium-chromedriver selenium-webdriver

我们有一个Ubuntu服务器,用于运行Chrome和Firefox的Selenium测试(我安装了ChromeDriver),我也想在Windows 10计算机上本地运行测试.我想保持两个计算机的Python代码相同.但我没有找到如何在Windows 10上安装ChromeDriver?我没有找到它的文档上[1 ,2] .

以下是在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)
        ...
Run Code Online (Sandbox Code Playgroud)

我还发现了如何在Chrome中运行Selenium WebDriver测试用例?但它似乎不是在Python中(没有标记编程语言,它是什么?)

更新#1:我在https://sites.google.com/a/chromium.org/chromedriver/getting-started中找到了一些Python代码,但是如果我想保留相同的Python,我在哪里将文件放在Windows 10中两台电脑的代码?

更新2:我下载并把chromedriver.exeC:\Windows和它的作品,但我没有看到它的任何地方记录.

Ada*_*rrh 18

正如Uri在问题中所述,在更新#2下,下载最新版本的chromedriver并将其置于C:\ Windows中可以解决问题.

当浏览器窗口打开时,我遇到了与Chrome挂起相同的问题(与命令提示符窗口一起).

最新的驱动程序可以在:

https://sites.google.com/a/chromium.org/chromedriver/downloads

chromedriver_win32.zip文件中的版本正在我的64位系统上运行.


小智 6

  1. 下载chromedriver.exe并将其保存到所需位置
  2. 指定executable_path到其保存路径

示例代码如下:

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()
Run Code Online (Sandbox Code Playgroud)

正如 Uri 在问题的更新 #2 中所述,如果我们将 放在chromedriver.exeC:/Windows,则无需指定,executable_path因为 Python 将在 下搜索C:/Windows


小智 5

我先简单介绍一下要求。您需要从此处下载 chrome 网络驱动程序 zip。https://chromedriver.storage.googleapis.com/index.html?path=2.33/

提取文件并将其存储在所需位置。

在 Eclipse 中创建一个新项目并在您的类中包含以下代码。

System.setProperty("webdriver.chrome.driver", "C:\\temp\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
Run Code Online (Sandbox Code Playgroud)

说明 : System.setProperty(key,value):

所有系统的键都是默认值,值是 chromedriver 提取文件的位置。