Setting up Firefox binary using Desired Capabilities

Viewed 8997

Sorry for the horrible formatting...i still did not figure out how to format nicely..

I am trying to launch firefox using Selenium. Firefox is installed in AppData folder. Hence I tried to set the Binary path on the Desired Capability object using the below code

System.setProperty("webdriver.gecko.driver","C:\\path\\geckodriver.exe");
String pathToBinary = "C:\\Users\\me\\AppData\\Local\\Microsoft\\AppV\\Client\\Integration\\D90C0155-81ED-4977-B52D-E34EAA24FB3C\\Root\\VFS\\ProgramFilesX86\\Mozilla Firefox\\firefox.exe";
ProfilesIni profile = new ProfilesIni();
FirefoxProfile testProfile = new FirefoxProfile();
testProfile.setAcceptUntrustedCertificates(true);
testProfile.setAssumeUntrustedCertificateIssuer(true);
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(FirefoxDriver.PROFILE, testProfile);
dc.setCapability(FirefoxDriver.BINARY, pathToBinary);
driver = new FirefoxDriver(dc);
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
//driver.manage().window().setSize(new Dimension(1920, 1080));

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
System.out.println("beforee driver.get");
driver.get("https://www.google.co.za");

I am having 2 issues here..

1. Getting below error on - driver.manage().window().maximize();
org.openqa.selenium.WebDriverException: Failed to find width field
Build info: version: '3.5.3', revision: 'a88d25fe6b', time: '2017-08-29T12:54:15.039Z'
System info: host: 'N0610114502', ip: '10.9.21.178', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0'
Driver info: org.openqa.selenium.firefox.FirefoxDriver

2. If I comment out that line and run the program, i get the below error
org.openqa.selenium.InvalidArgumentException: 
Build info: version: '3.5.3', revision: 'a88d25fe6b', time: '2017-08-29T12:54:15.039Z'
System info: host: 'N0610114502', ip: '10.9.21.178', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{moz:profile=C:\Users\cc305718\AppData\Local\Temp\rust_mozprofile.sIobxPYVJ8iA, rotatable=false, timeouts={implicit=0, page load=300000, script=30000}, pageLoadStrategy=normal, platform=XP, specificationLevel=0, moz:accessibilityChecks=false, acceptInsecureCerts=true, browserVersion=52.5.0, platformVersion=6.1, moz:processID=6712, browserName=firefox, javascriptEnabled=true, platformName=XP}]
Session ID: 030d91eb-9219-43c9-9ade-210005b150b8

Does this mean I am setting the Binary in a wrong way? If so what is the correct usage?

I am not keen to use the below depricated method

FirefoxProfile profile = new FirefoxProfile(); 
FirefoxBinary binary = new FirefoxBinary(new File("C:\\path to firefox\\firefox.exe")); 
driver = new FirefoxDriver(binary, profile);
1 Answers

For the first error you're seeing:

org.openqa.selenium.WebDriverException: Failed to find width field

I believe this has happened to you because you're asking Selenium to maximize the browser window without telling it what size you want the browser window to be when it's maximized. So it's failing with "failed to find width field" since you've not actually set that width parameter prior to this.

So you'll need the line of code, below, to be before the call to maximize the window (you have currently commented this out in your sample code, above).

driver.manage().window().setSize(new Dimension(1920, 1080));

Try it out with that first error fixed and if you're still having any issues please feel free to ask me. Glad to help if I can!

Update 1:

Right my next suggestion is to re-order your code slightly at the point where you're creating your WebDriver instance. I just want to ensure that everything is in the right order (I've seen issues with the order of statements with Selenium before) and whether you're still seeing the error(s):

driver = new FirefoxDriver(dc);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.get("https://www.google.co.za");
driver.manage().window().setSize(new Dimension(1920, 1080));
driver.manage().window().maximize();

Update 2:

OK I'm satisfied that there's nothing wrong with how we're now calling your WebDriver instance so there is something amiss with how you are setting up your gecko driver.

I notice you are setting a binary for your actual Firefox installation in both of these lines:

String pathToBinary = "C:\\Users\\me\\AppData\\Local\\Microsoft\\AppV\\Client\\Integration\\D90C0155-81ED-4977-B52D-E34EAA24FB3C\\Root\\VFS\\ProgramFilesX86\\Mozilla Firefox\\firefox.exe";
dc.setCapability(FirefoxDriver.BINARY, pathToBinary);

You will only these to be set if the path to your Firefox binary isn't on your PATH. So you can comment out / remove both of these lines if it's on your PATH or keep them there if not.

You should also set the marionette capability to true as follows:

dc.setCapability("marionette", true);

So just to give you a summary of how you should now be setting up your WebDriver instance:

DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(dc);
driver.get("https://www.google.co.za");
Related