gpt4 book ai didi

java - 获取错误为 "method chrome() is undefined for the type DesiredCapabilities"

转载 作者:行者123 更新时间:2023-11-30 12:02:36 26 4
gpt4 key购买 nike

上下文:从以下代码中获取错误,因为方法 chrome 未定义:

package zapSeleniumIntegration;

import java.lang.reflect.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

public class BrowserDriverFactory {

static WebDriver driver;
public static WebDriver createChromeDriver(Proxy proxy, String path) {
// Set proxy in the chrome browser
DesiredCapabilities capabilities = DesiredCapabilities.chrome();//Getting error as method chrome undefined
capabilities.setCapability("127.0.0.1", 8080);
// Set system property for chrome driver with the path
System.setProperty("webdriver.chrome.driver", "C:\\Users\\kotla.naveen\\Desktop\\chromedriver.exe");
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
ChromeOptions options = new ChromeOptions();
options.merge(capabilities);
return new ChromeDriver(options);
}
}

最佳答案

The old method Capabilities is deprecated. Use ChromeOptions object and passed it to the ChromeDriver() constructor. Also, you don't need to use options.merge(capabilities); instead, you can use options.setCapability(params, params)

但是,您可以完全跳过 DesiredCapabilities 并仅使用 ChromeOptions 和 setCapability 方法,如下所述:

ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");
options.setAcceptInsecureCerts(true);
options.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.ACCEPT);

//设置预定义的能力——ACCEPT_SSL_CERTS值为真:

options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

//将 chromeoption 对象传递给 ChromeDriver:

WebDriver driver = new ChromeDriver(options);

************** 设置代理 **************

ChromeOptions options = new ChromeOptions();
Proxy proxy = new Proxy();
proxy.setAutodetect(false);
proxy.setHttpProxy("proxy_url:port");
proxy.setSslProxy("proxy_url:port");
proxy.setNoProxy("no_proxy-var");

options.setCapability("proxy", proxy);
driver = new ChromeDriver(options);

关于java - 获取错误为 "method chrome() is undefined for the type DesiredCapabilities",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58411791/

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