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