我正在使用Web驱动程序管理器来设置chrome驱动程序。设置驱动程序时,我想添加一些铬选项?使用 Web 驱动程序管理器时如何执行此操作?
我检查了WebDriverManager API,但找不到任何线索。
从WebDriverManager 5.x开始,你可以通过WebDriverManager构建器直接实例化webDriver,并以这种方式(在java中(附加capabilities
:
WebDriver driver;
//...
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
//...
//chromeOptions.addArguments(<another-option>);
//...
driver = WebDriverManager.chromedriver().capabilities(chromeOptions).create();
capabilities方法将Capabilities
作为param.
幸运的是,ChromeOptions
实现了Capabilities
接口。
public void WebDriverManagerTest()
{
//setup the chromedriver using WebDriverManager
WebDriverManager.chromedriver().setup();
//Create Chrome Options
ChromeOptions option = new ChromeOptions();
option.addArguments("--test-type");
option.addArguments("--disable-popup-bloacking");
DesiredCapabilities chrome = DesiredCapabilities.chrome();
chrome.setJavascriptEnabled(true);
option.setCapability(ChromeOptions.CAPABILITY, option);
//Create driver object for Chrome
WebDriver driver = new ChromeDriver(option);
//Navigate to a URL
driver.get("http://toolsqa.com");
//quit the browser
driver.quit();
}
找到答案..检查上面!
这是示例代码:
public class Test1{
@Test
public void WebDriverManagerTest()
{
//setup the chromedriver using WebDriverManager
WebDriverManager.chromedriver().setup();
//Create driver object for Chrome
WebDriver driver = new ChromeDriver();
//Navigate to a URL
driver.get("http://toolsqa.com");
//quit the browser
driver.quit();
}
}
从 https://pypi.org/project/webdriver-manager/中传入,在 .install(( 之后传入
from selenium import webdriver
from webdriver_manager.opera import OperaDriverManager
options = webdriver.ChromeOptions()
options.add_argument('allow-elevated-browser')
options.binary_location = "C:\Users\USERNAME\FOLDERLOCATION\Opera\VERSION\opera.exe"
driver = webdriver.Opera(executable_path=OperaDriverManager().install(), options=options)
在Pom中.xml添加Web驱动程序管理器依赖项,并在创建浏览器实例时,您可以使用以下截取的代码在隐身状态下运行,不受自动化控制。
public void setUpChromeBrowser(){
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
options.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
options.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
options.setExperimentalOption("useAutomationExtension", false);
options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
options.addArguments("--no-sandbox");
options.addArguments("--ignore-certificate-errors");
options.addArguments("--start-maximised");
options.addArguments("--incognito");
options.setExperimentalOption("w3c", false);
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("credentials_enable_service", false);
prefs.put("profile.password_manager_enabled", false);
options.setExperimentalOption("prefs", prefs);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(options);
}