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");
	}
}

 

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