与WebdriverManager和ChromeOptions一起使用时如何修复Selenium DesiredCapabilities导致错误“未解析为类型”

问题描述

如何修复与WebdriverManager和ChromeOptions一起使用时出现错误“未解析为类型”的Selenium DesiredCapabilities

package pack.tests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.BeforeTest;
import org.openqa.selenium.remote.CapabilityType;
//Error The import org.openqa.selenium.remote.CapabilityType cannot be resolved
import org.openqa.selenium.remote.DesiredCapabilities;
//Error The import org.openqa.selenium.remote.DesiredCapabilities cannot be resolved
import io.github.bonigarcia.wdm.WebDriverManager;
public class TestGuru99Login{
@BeforeTest
public void Setup() {
        WebDriverManager.chromedriver().setup();
        ChromeOptions option = new ChromeOptions();
        option.addArguments("--test-type");
        option.addArguments("--disable-popup-bloacking");
        option.addArguments("--incognito");
        DesiredCapabilities chrome = DesiredCapabilities.chrome();
        /*Error
        Multiple markers at this line - DesiredCapabilities cannot be resolved -
        * DesiredCapabilities cannot be resolved to a type*/
        chrome.setJavascriptEnabled(true);
        chrome.setCapability(ChromeOptions.CAPABILITY,option);
        WebDriver driver = new ChromeDriver(option);
        //Error:Type mismatch: cannot convert from ChromeDriver to WebDriver
        driver.get("https://toolsqa.com");
        driver.quit();
    }
}

解决方法

org.openqa.selenium.remote.DesiredCapabilities 上面的一个来自硒远程驱动程序模块。您可以在POM.xml中添加以下内容,然后尝试一下。您可以根据与其他硒模块的兼容性来更改“版本”号。

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-remote-driver</artifactId>
    <version>4.0.0-alpha-3</version>
</dependency>
,

enter image description here

此解决方案似乎适用于我。面对此问题的其他任何人,上述所有其他解决方案均无效。

,

当您在ModulePath中添加了外部jar时,就会发生这种情况。 解: 在此处输入代码从节点“ Modulepath”中删除外部jar。选择节点“ Classpath”,然后添加外部jar。检查硒罐是否已导入。