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

使用chrome driver和chrome浏览器并进入chrome的 toggle device mode 模式,就可以很好的模拟手机端,下面直接上代码

			System.setProperty("webdriver.chrome.driver", config.getProperty("chromePath"));
		        Map mobileEmulation = new HashMap();  
		        mobileEmulation.put("deviceName", "Apple iPhone 6");  
		        Map chromeOptions = new HashMap();     
		        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)