瀏覽器:
Chrome
首先通過developer模式查看Chrome瀏覽器支持哪些手機,如圖:
在代碼中使用ChromeOptions對象的addArguments方法來設置參數,如下代碼所示:
package test; import org.junit.After; import org.junit.Before; import org.openqa.selenium.Dimension; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; public class Test { @Before public void setUp() throws Exception { } @After public void tearDown() throws Exception { } @org.junit.Test public void test() { // 聲明ChromeOptions,主要是給Chrome設置參數. final ChromeOptions chromeOptions = new ChromeOptions(); // 設置user agent的參數為iPhone 6 chromeOptions.addArguments("--user-agent=iPhone 6"); final WebDriver driver = new ChromeDriver(chromeOptions); // 設置打開的瀏覽器的屏幕大小,模擬手機屏幕. driver.manage().window().setSize(new Dimension(600, 900)); driver.get("https://meitaichina.com"); } }