有什么方法可以轻松地将 ChromeDriver 添加到程序中

Is there any way to easily add ChromeDriver to a program

我发现自己经常使用 Selenium 来实现 Web 自动化,所以我最终也经常使用 ChromeDriver。我目前如何将它添加到我的所有项目中是我制作 chromedriver.exe 的副本并将副本添加到我的所有项目中。然后我用下面的代码告诉我的程序 chromedriver.exe 在哪里:

String userPath = System.getProperty("user.dir");
System.setProperty("webdriver.chrome.driver", userPath + "/chromedriver.exe");

为每个项目制作一个新的.exe很烦人,我想知道是否有更好的方法

我知道很多人建议在您的计算机上安装一个 chromedriver.exe,然后设置驱动程序的路径并执行如下操作:

System.setProperty("webdriver.chrome.driver", "C:\Users\User\Path\chromedriver.exe");

我的问题是,如果我想将程序移动到另一台计算机或服务器上,我需要将此路径更改为 chromedriver.exe.

的新实例

我想知道是否可以将 chromedriver 上传到 github,然后将路径设置到您存储 chromedriver.exe 的存储库,所以您可能有这样的东西:

System.setProperty("webdriver.chrome.driver", "https://github.com/Tryanno5/chromeDriver/blob/main/chromedriver.exe");

(这个不行,我已经试过了,这只是一个例子)

有没有什么方法可以使 chromedriver.exe 在线并始终可以访问? 或者有没有更好的方法可以将它应用到所有项目中,而不必总是在我的计算机上引用特定的副本?

您尝试过 WebDriverManager 检查 here

添加依赖

<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>4.3.0</version>
</dependency>

安装驱动程序

WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com")

如果是 python,会更容易,因为有一个自动安装程序 API,请参阅此处 -

安装

pip install chromedriver-autoinstaller

用法

Just type import chromedriver_autoinstaller in the module you want to use chromedriver.

例子

from selenium import webdriver
import chromedriver_autoinstaller


chromedriver_autoinstaller.install()  # Check if the current version of chromedriver exists
                                      # and if it doesn't exist, download it automatically,
                                      # then add chromedriver to path

driver = webdriver.Chrome()
driver.get("http://www.python.org")
assert "Python" in driver.title

more

请看这里