如何在没有ChromeDriver.exe的Maven中使用Selenium-chrome-driver [英] How to work with selenium-chrome-driver in Maven without ChromeDriver.exe
问题描述
我添加了以下依赖项和用于打开Chrome的代码,但浏览器未打开.
I add the below dependency and code for Opening Chrome,but browser is not opening.
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>2.50.0</version>
</dependency>
我的代码:-
package example;
import org.openqa.selenium.WebDriver;`
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class DepChrome {
@Test
public void testBrowser() {
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
}
}
推荐答案
添加以下依赖项:
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.0.0</version>
<!-- <scope>test</scope> -->
</dependency>
来源:从下面的URL复制新的依赖项版本:
Source: copy new dependencies version from below URL:
https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager
使用以下代码:
WebDriver driver = null;
WebDriverManager.chromedriver().version("77.0.3865.40").setup();
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("enable-automation");
options.addArguments("--no-sandbox");
options.addArguments("--disable-infobars");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--disable-browser-side-navigation");
options.addArguments("--disable-gpu");
driver = new ChromeDriver(options);
driver.get("https://www.google.com/");
基本上,下面的代码行完成了这个技巧,下面的代码下载了特定版本
Basically below line of code did the trick, below code to download a specific version
WebDriverManager.chromedriver().version("77.0.3865.40").setup();
您可以从以下URL获取所需的版本:
Required version you can get from below URL:
https://chromedriver.storage.googleapis.com/index.html
如果要查找上面chromedriver URL上最新的依赖项,也可以使用下面的代码代替上面的代码
you can also use below code instead of above, if you are looking for latest dependencies present on above chromedriver URL
WebDriverManager.chromedriver().setup();
OR(旧方法)
您需要给出chrome二进制文件的路径,如下所示:
You need to give path of chrome binary as below:
System.setProperty("webdriver.chrome.driver", "C:\\pathto\\my\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
从硒站点下载铬的二进制文件,如下所示:- http://chromedriver.storage.googleapis.com/index.html?path= 2.21/
Download the binary of chrome from selenium site as below :- http://chromedriver.storage.googleapis.com/index.html?path=2.21/
现在提供二进制文件到硒的路径:-
Now provide the path of the binary to selenium as :-
System.setProperty("webdriver.chrome.driver", "C:\\pathto\\my\\chromedriver.exe");
还有一件事情要注意.如果使用的是Windows,则使用反斜杠\\
;如果使用的是Mac或linux,则使用正斜杠//
设置路径.
There is one more thing to take care. if you are using windows then use backward slash \\
and if you are using mac or linux then use forward slash //
for setting up the path.
希望它会为您提供帮助:)
Hope it will help you :)
这篇关于如何在没有ChromeDriver.exe的Maven中使用Selenium-chrome-driver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!