以下是我用于ChromeDriver的设置
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
// Add the WebDriver proxy capability.
Proxy proxy = new Proxy();
proxy.setHttpProxy("127.0.0.1:3128");
proxy.setSslProxy("127.0.0.1:3128");
capabilities.setCapability("proxy", proxy);
// Add ChromeDriver-specific capabilities through
// ChromeOptions.
ChromeOptions options = new ChromeOptions();
options.addArguments("--incognito");
options.addArguments("--disable-bundled-ppapi-flash");
options.addArguments("--disable-extensions");
options.addArguments("--profile-directory=Default");
options.addArguments("--disable-plugins-discovery");
options.addArguments("--start-maximized");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
但我也想添加移动模拟。根据我在文档中读到的内容,以下是我需要的内容
Map<String, String> mobileEmulation = new HashMap<String, String>();
mobileEmulation.put("deviceName", "Google Nexus 5");
Map<String, Object> chromeOptions = new HashMap<String, Object>();
chromeOptions.put("mobileEmulation", mobileEmulation);
然后我会将其设置为如下所示的功能
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
然后将其传递给ChromeDriver。但是,如果我将其设置为DesiredCapabilities,那么我的代理和其他选项将不再应用于DesiredCapabilities。
如何在保留代理和选项设置的同时模拟移动设备?
答案 0 :(得分:0)
您可以通过密钥chromeOptions
和args
参数将参数传递到List<String>
:
结合mobileEmulation
设置,这应保留所有选项设置。
Map<String, String> mobileEmulation = new HashMap<String, String>();
mobileEmulation.put("deviceName", "Google Nexus 5");
Map<String, Object> chromeOptions = new HashMap<String, Object>();
chromeOptions.put("mobileEmulation", mobileEmulation);
// add this
chromeOptions.put("args", Arrays.asList("incognito", "disable-bundled-ppapi-flash",
"disable-extensions", "profile-directory=Default", "disable-plugins-discovery",
"start-maximized"));
此外,您的代理设置不应受影响,您在setCapability
上呼叫ChromeOptions.CAPABILITY
,"proxy"
设置保持不变。