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

wylc123 1年前 ⋅ 499 阅读

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

JAVA版本

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

https://www.cnblogs.com/iamhp/p/6016194.html

Python版本

from selenium.webdriver.chrome.options import Options
chrome_options = Options()
#设置user agent为iphone6plus
mobile_emulation = {"deviceName":"iPhone 6"}
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
chrome_options.add_argument("--headless")
更多内容请访问:IT源点

相关文章推荐

全部评论: 0

    我有话说: