1

I will use selenium remote web driver for test my web application. In my case I should be use firefox web driver. Now I don't know how canI change my useragent in this case

It is my code for use remote web driver

DesiredCapabilities Capabilities = new DesiredCapabilities();
Capabilities.SetCapability(CapabilityType.BrowserName, "firefox");                
string GridURL = "http://localhost:4545/wd/hub";
driver = new RemoteWebDriver(new Uri(GridURL), Capabilities);
llinvokerl
  • 1,029
  • 10
  • 25

1 Answers1

0

You can't directly change the RemoteWebDriver. RemoteWebDriver c# Documentation. From the documentation, you can see that the RemoteWebDriver is an abstraction of more concrete implementations like FirefoxDriver and ChromeDriver.

These imlementations expose preferences like the user.agent. What the api is trying to tell you, is that not all RemoteWebDrivers need to have preferences and thus it's not part of the class.

You can however do the following since FirefoxDriver extends RemoteWebDriver

RemoteWebDriver driver = new FirefoxDriver(profile);

You can use the FirefoxProfile and a FirefoxDriver

FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("general.useragent.override", "Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25");
IWebDriver driver = new FirefoxDriver(profile);

A full example:

public static IWebDriver GetDriver(string driver, Devices device)
 {
     DeviceModel model = Device.Get(device);
     IWebDriver webDriver;
     switch (driver.ToLower())
     {
         case "safari":
             webDriver = new SafariDriver();
             break;
         case "chrome":
             webDriver = new ChromeDriver();
             break;
         case "ie":
             webDriver = new InternetExplorerDriver();
             break;
         //case "firefox":
         default:
             var profile = new FirefoxProfile();
             profile.SetPreference("general.useragent.override", model.UserAgent);
             webDriver = new FirefoxDriver(profile);
             webDriver.Manage().Window.Size = model.ScreenSize;
             break;
     }
     return webDriver;
 }
Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
  • i will change user agent in remoter webdriver no in firefox webdriver!!!! – user11824390 Jan 04 '20 at 08:12
  • You can't. https://selenium.dev/selenium/docs/api/dotnet/html/T_OpenQA_Selenium_Firefox_FirefoxDriver.htm firefox driver extends remote web driver. Check the documentation, the remote web driver has nothing to expose the profile. – Athanasios Kataras Jan 04 '20 at 08:20
  • By the way, you are using RemoteWebDriver when you use FirefoxDriver. You can even assign the FirefoxDriver to a RemoteWebDriver without casting. – Athanasios Kataras Jan 04 '20 at 08:21