gpt4 book ai didi

java - 在selenium中打开浏览器时出现异常

转载 作者:行者123 更新时间:2023-12-01 19:37:37 25 4
gpt4 key购买 nike

我已经应用了所有代码,但在 selenium 中打开 chrome 浏览器时仍然出现错误。我也为 gecko-driver 设置了属性。请检查代码并给出一些解决方案

我收到此错误

Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html

at com.google.common.base.Preconditions.checkState(Preconditions.java:847) at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:134) at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:35) at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:159) at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:355) at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:94) at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:123) at hps1.HPS.main(HPS.java:10)

HPS.java

package hps1;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class HPS {

public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver=new ChromeDriver();

//System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver_win32\\chromedriver.exe");
// Open
driver.get("http://www.facebook.com");

// Maximize browser
driver.manage().window().maximize();
}

}

最佳答案

您首先初始化 驱动程序,然后设置系统属性,这就是它抛出错误的原因。将属性设置行移至驱动程序初始化上方即可为您完成这项工作。

public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();

// Open
driver.get("http://www.facebook.com");

// Maximize browser

driver.manage().window().maximize();

}

关于java - 在selenium中打开浏览器时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56863248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com