修改User-Agent来伪装浏览器访问手机站点


有时候为了测试需要,可能需要使用测试手机wap这样的站点,如果用真正的手机去测试也可以实现,但是比较麻烦,我们可以通过设置chrome的user agent来伪装浏览器,达到我们的测试目的。

 代码如下:

 public static void main(String[] args) {
        //设置webdriver.chrome.driver属性
        System.setProperty("webdriver.chrome.driver", "d:/driver/chromedriver.exe");
        //声明chromeoptions,主要是给chrome设置参数
        ChromeOptions options = new ChromeOptions();
        //设置user agent为iPhone5
        options.addArguments("--user-agent=Apple Iphone 5");
        //实例化chrome对象,并加入选项
        WebDriver driver = new ChromeDriver(options);
        //打开百度
        driver.get("https://www.baidu.com");
        try{
            Thread.sleep(5000);
        }catch(InterruptedException e) {
            e.printStackTrace();
        }
        driver.quit();
    }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM