Changing the user agent using selenium webdriver in Java

后端 未结 3 1506
再見小時候
再見小時候 2021-02-06 11:36

Can someone pls tell me how to switch the user agent using webdriver in Java? I tried below, but getting errors.

FirefoxProfile ffp = new FirefoxProfile(); 
ffp.         


        
3条回答
  •  独厮守ぢ
    2021-02-06 12:20

    I believe this solution is the desired answer to the question. I tested it and it worked for me. Happy coding!

    FirefoxOptions options = new FirefoxOptions();
    String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36 OPR/60.0.3255.170";
    options.addPreference("general.useragent.override",userAgent);
    
    WebDriver webDriver = new FirefoxDriver(options);
    webDriver.get("http://whatsmyuseragent.org");
    

提交回复
热议问题