使用ChromeOptions设置浏览器为移动端模式时报错
selenium吧
全部回复
仅看楼主
level 2
系统信息如下:
System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_172'
Chrome version: 89.0.4389.82
Chrome Driver version : chromedriver_win32.zip 2021-01-28T17:30:57.154Z
Selenium version :3.141.59
代码如下:
public void setUp() throws IOException, InterruptedException {
String startChromeCMD = "chrome --remote-debugging-port=9222";
// 启动chrome浏览器
Runtime.getRuntime().exec(startChromeCMD);
Thread.sleep(1000);
System.setProperty("webdriver.chrome.driver", DRIVER_TOOLS_DIR);
//配置参数
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir", CHROME_WORK_DIR);
options.setExperimentalOption("debuggerAddress", DRIVER_DEBUG_ADDRESS);
Map<String, Object> mobileEmulation = new HashMap<String, Object>();
mobileEmulation.put("deviceName", "Moto G4");
options.setExperimentalOption("mobileEmulation", mobileEmulation);
driver = new ChromeDriver(options);
driver.manage().window().setSize(new Dimension(1280, 890));
}
结果程序运行时报错,提示出错行是这一行:driver = new ChromeDriver(options);
查了很多信息,都说是driver与chrome不对,但我是按关联关系下载的,其它功能都可以正常使用,但唯独模拟移动模式不可以。应该不是版本不符造成的。实在没撤了,救助。
异常信息如下:
org.openqa.selenium.InvalidArgumentException: invalid argument: entry 0 of 'firstMatch' is invalid
from invalid argument: cannot parse capability: goog:chromeOptions
from invalid argument: unrecognized chrome option: mobileEmulation
remote stacktrace: Backtrace:
GetHandleVerifier [0x00EFB963+483]
GetHandleVerifier [0x00EFB941+449]
GetHandleVerifier [0x012A3308
+3
832712]
GetHandleVerifier [0x012B5190+3906064]
GetHandleVerifier [0x012B7724+3915684]
GetHandleVerifier [0x012B29AE+3895854]
GetHandleVerifier [0x012DDA42+4072130]
GetHandleVerifier [0x012DD5BE+4070974]
GetHandleVerifier [0x012DE6BB+4075323]
GetHandleVerifier [0x012DE56C+4074988]
GetHandleVerifier [0x012DB2EB+4062059]
GetHandleVerifier [0x012BED14+3945876]
GetHandleVerifier [0x012BFBCE+3949646]
GetHandleVerifier [0x012BFB59+3949529]
Ordinal0 [0x00EBB5CC+46540]
Ordinal0 [0x00EB9F53+40787]
Ordinal0 [0x00EB9B12+39698]
GetHandleVerifier [0x011C1468+2907368]
GetHandleVerifier [0x010071EE+1096302]
GetHandleVerifier [0x00FC3E8D+821005]
GetHandleVerifier [0x00FC396B+819691]
GetHandleVerifier [0x00FC3881+819457]
GetHandleVerifier [0x00FEF463+998627]
BaseThreadInitThunk [0x746F62C4+36]
RtlSubscribeWnfStateChangeNotification [0x775F0FD9+1081]
RtlSubscribeWnfStateChangeNotification [0x775F0FA4+1028]
2021年03月09日 09点03分 1
level 2
后来发现,只要添加了这两个参数,再添加 mobileEmulation 参数后就会报错:
options.addArguments("user-data-dir", CHROME_WORK_DIR);
options.setExperimentalOption("debuggerAddress", DRIVER_DEBUG_ADDRESS);
如果不使用上面两个参数就不会报错,但是这几个参数是必须要的,
放这个是为了使用指定的存放数据路径
options.addArguments("user-data-dir", CHROME_WORK_DIR);
放这个是为了使用指定的端口,否则会弹出两个chrome窗口
options.setExperimentalOption("debuggerAddress", DRIVER_DEBUG_ADDRESS);
真是不知道该怎么解决了,求大神指点。
2021年03月10日 02点03分 4
解决了吗?我也出现了这个错误
2021年06月10日 05点06分
@七秒鱼与猫💋 我的python上把设备换成iPhone SE,就好了
2022年04月13日 09点04分
忘了解决没了,后来直接转战andriod app了
2023年02月13日 07点02分
level 1
感觉多试几个设备就好了
2022年04月13日 09点04分 5
1