gpt4 book ai didi

java - 通过 webdrivermanager 使用 Selenium 运行 Opera 浏览器时出现问题

转载 作者:太空宇宙 更新时间:2023-11-04 10:00:15 26 4
gpt4 key购买 nike

我在使用 Selenium 的 Opera 浏览器中遇到问题。要下载浏览器二进制文件,我使用 webdrivermanager 插件( https://github.com/bonigarcia/webdrivermanager )

这是一个实现不同类型浏览器的枚举:

public enum DriverType implements DriverSetup {

CHROME {
public WebDriver getWebDriverObject() {
WebDriverManager.chromedriver().setup();
return new ChromeDriver();
}
},
FIREFOX {
public WebDriver getWebDriverObject() {
WebDriverManager.firefoxdriver().setup();
return new FirefoxDriver();
}
},
OPERA {
public WebDriver getWebDriverObject() {
WebDriverManager.operadriver().setup();
return new OperaDriver();
}
}
}

调用了 getWebDriverObject 方法:

public class DriverBase {

public WebDriver driver;

public WebDriver initializeBrowser() {
DriverType driverType = DriverType.CHROME;

try {
String browser = System.getProperty("browser").toUpperCase();
driver = DriverType.valueOf(browser).getWebDriverObject();
} catch (Exception e) {
System.out.println("No driver specified/Wrong driver specified, default tests running on Chrome");
driver = driverType.getWebDriverObject();
}
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
return driver;
}
}

在运行测试之前,我将系统属性浏览器设置为“opera”。这是一个输出。测试默认在 chrome 中运行,opera 浏览器未打开,因此无法运行。我该如何修复它?

    ProjectName > Login STANDARD_ERROR
1160 [Test worker] WARN io.github.bonigarcia.wdm.WebDriverManager - The driver version for Opera No Instance(s) Available. is unknown .
.. trying with latest
1163 [Test worker] INFO io.github.bonigarcia.wdm.WebDriverManager - Reading https://api.github.com/repos/operasoftware/operachromiumdri
ver/releases to seek operadriver
1465 [Test worker] INFO io.github.bonigarcia.wdm.WebDriverManager - Latest version of operadriver is 2.41
1548 [Test worker] INFO io.github.bonigarcia.wdm.Downloader - Using binary driver previously downloaded
1550 [Test worker] INFO io.github.bonigarcia.wdm.WebDriverManager - Exporting webdriver.opera.driver as C:\Users\me\.m2\re
pository\webdriver\operadriver\win64\2.41\operadriver.exe
Starting OperaDriver 2.41 on port 37865
Only local connections are allowed.

Socrates > Login STANDARD_OUT
No driver specified/Wrong driver specified, default tests running on Chrome

最佳答案

此错误消息...

INFO io.github.bonigarcia.wdm.WebDriverManager - Exporting webdriver.opera.driver as C:\Users\me\.m2\repository\webdriver\operadriver\win64\2.41\operadriver.exe
Starting OperaDriver 2.41 on port 37865
Only local connections are allowed.

Socrates > Login STANDARD_OUT
No driver specified/Wrong driver specified, default tests running on Chrome

...意味着虽然您的程序按照系统属性配置访问了OperaDriver,但由于驱动程序规范不匹配而调用了Chrome浏览器

您的主要问题是,尽管您已将系统属性浏览器设置为opera,但仍在initializeBrowser()方法中:

DriverType driverType = DriverType.CHROME;

解决方案

按如下方式更改 initializeBrowser() 方法:

public WebDriver initializeBrowser() {
DriverType driverType = DriverType.OPERA;

try {
String browser = System.getProperty("browser").toUpperCase();
driver = DriverType.valueOf(browser).getWebDriverObject();
} catch (Exception e) {
System.out.println("No driver specified/Wrong driver specified, default tests running on Chrome");
driver = driverType.getWebDriverObject();
}

更新

现在您看到的错误是:

org.openqa.selenium.WebDriverException: unknown error: cannot find Opera binary

要解决此问题,您可以关注讨论 selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Opera binary with OperaDriver Selenium and Python

关于java - 通过 webdrivermanager 使用 Selenium 运行 Opera 浏览器时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53616350/

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