浏览器设置代码(以chrome浏览器为例)
//设置浏览器driver
System.setProperty("webdriver.chrome.driver", "tools/chromedriver.exe");
//浏览器选项设置
ChromeOptions options = new ChromeOptions();
options.addArguments("lang_zh_CN.UTF-8");
//这步是关键哦,指定的浏览器size,对应手机型号的size
options.addArguments("window-size=375,667");
options.addArguments("user-agent=\"Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Version/11.0 Mobile/15C114 Safari/604.1\"");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(options);
// driver.manage().window().maximize();
driver.get("https://www.baidu.com");
运行后打开的效果见下图:
原文地址:http://blog.51cto.com/4998165/2097491