selenium中将chrome浏览器设置成手机模式

chrome浏览器可以模拟手机模式,打开chrome,然后按F12,然后点击下图中红框中手机的标识,切换成手机模式

点击Edit可以增加不同的手机型号

设置手机模式为苹果6plus,代码如下:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

/*
 * 将chrome浏览器设置成手机模式
 */

public class KeywordBrowserChromeUserAgent {

	public static void main(String[] args) {	 
		 //声明ChromeOptions,主要是给chrome设置参数
		 ChromeOptions options = new ChromeOptions();
		 
		 //设置user agent为iphone6plus
		 options.addArguments("--user-agent=iphone 6 plus");
		 
		 //设置webdriver.chrome.driver属性
		 System.setProperty("webdriver.chrome.driver", "D:\\chromedriver\\chromedriver.exe");
		 
		 WebDriver driver = new ChromeDriver(options);
		 
		 driver.get("http://www.baidu.com");
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章