Java 将错误获取为";对于类型DesiredCapabilities,未定义方法chrome();
上下文:从以下代码中获取错误作为未定义的方法:Java 将错误获取为";对于类型DesiredCapabilities,未定义方法chrome();,java,selenium,selenium-chromedriver,desiredcapabilities,Java,Selenium,Selenium Chromedriver,Desiredcapabilities,上下文:从以下代码中获取错误作为未定义的方法: 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.sele
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);
}
}
旧方法功能已被弃用。
使用ChromeOptions对象并将其传递给ChromeDriver()
构造函数。另外,您不需要使用options.merge(功能)代码>相反,您可以使用options.setCapability(params,params)
但是,您可以完全跳过所需的功能,只使用ChromeOptions和setCapability方法,如下所述:
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");
options.setAcceptInsecureCerts(true);
options.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.ACCEPT);
options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
WebDriver driver = new ChromeDriver(options);
//将预定义功能–ACCEPT\u SSL\u CERTS值设置为true:
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");
options.setAcceptInsecureCerts(true);
options.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.ACCEPT);
options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
WebDriver driver = new ChromeDriver(options);
//将chromeoption对象传递给ChromeDriver:
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");
options.setAcceptInsecureCerts(true);
options.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.ACCEPT);
options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
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);