您的位置:首页 > 编程语言 > Java开发

selenium以手机浏览模式打开浏览器

2017-02-22 10:25 253 查看
使用chrome driver和chrome浏览器并进入chrome的 toggle device mode 模式,就可以很好的模拟手机端,下面直接上代码

System.setProperty("webdriver.chrome.driver", config.getProperty("chromePath"));
Map<String, String> mobileEmulation = new HashMap<String, String>();
mobileEmulation.put("deviceName", "Apple iPhone 6");
Map<String, Object> chromeOptions = new HashMap<String, Object>();
chromeOptions.put("mobileEmulation", mobileEmulation);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);

driver=new MobileChromeDriver(capabilities);

这里MobileChromeDriver是为了方便使用TouchActions,只需新建一个类继承自ChromeDriver并实现HasTouchScreen接口。

public class MobileChromeDriver extends ChromeDriver implements HasTouchScreen {
private RemoteTouchScreen touch;

public MobileChromeDriver(Capabilities capabilities) {
super(capabilities);
touch = new RemoteTouchScreen(getExecuteMethod());
}

public TouchScreen getTouch() {
return touch;
}
}

转载自:http://www.cnblogs.com/iamhp/p/6016194.html
http://www.cnblogs.com/g-song/p/5205972.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  selenium 手机模式 java