Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to start Firefox 62 with extensions #6548

Closed
AndreSteenbergen opened this issue Oct 17, 2018 · 8 comments
Closed

Unable to start Firefox 62 with extensions #6548

AndreSteenbergen opened this issue Oct 17, 2018 · 8 comments

Comments

@AndreSteenbergen
Copy link

OS: Windows 10
Selenium Version: Selenium 3.14
Browser: Firefox 62
Browser Version: 62.0.3 (64-bits)
Geckodriver: 0.23, 0.22, 0.21, 0.20

Expected Behavior -

Working firefox with extension enabled

Actual Behavior -

No extensions enabled

Steps to reproduce -

Create a Firefox driver using the follwoing code, and check if the extension is loaded

public static IWebDriver GetFirefoxDriver()
       {
           Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

           var exePath = Assembly.GetExecutingAssembly().Location;
           var directory = Path.GetDirectoryName(exePath);

           Console.WriteLine("Trying to start Firefox in directory: " + directory);

           FirefoxOptions ffOptions = new FirefoxOptions();
           ffOptions.BrowserExecutableLocation = "C:\\Program Files\\Mozilla Firefox\\firefox.exe";

           FirefoxProfile profile = new FirefoxProfile();
           profile.DeleteAfterUse = true;
           profile.AddExtension(Path.Combine(directory, "ublock.xpi"));

           ffOptions.Profile = profile;
           ffOptions.AcceptInsecureCertificates = true;
           
           var driver = new FirefoxDriver(directory, ffOptions);
           
           //1366×768
           var padding = (ReadOnlyCollection<object>)driver.ExecuteScript("return [window.outerWidth-window.innerWidth, window.outerHeight-window.innerHeight];");
           driver.Manage().Window.Size = new Size(1366 + int.Parse(padding[0].ToString()), 768 + int.Parse(padding[0].ToString()));

           return driver;
       }
@miksch123
Copy link

I am able to verify the issue you are seeing. No other way other than using Firefox 61 seems to help.

@AndreSteenbergen
Copy link
Author

AndreSteenbergen commented Oct 22, 2018

Is this a selenium issue, geckodriver issue or firefox issue? If ff 61 works, I guess some security stuff in ff 62?

@AndreSteenbergen
Copy link
Author

Figured it out ... The xpi files are extracted in the extensions folder. I just downloaded the sources, the Install method for xpi's should not be extracting the xpi, but simply copying the xpi. I altered FirefoxExtension.cs to copy the xpi file. Now it works. I don't know if that will work for older versions of firefox. I only have the latest version installed. I am a little reluctant to cerate a PR. This is my change to get it working:

public void Install(string profileDirectory)
        {
            DirectoryInfo info = new DirectoryInfo(profileDirectory);

            var targetFile = Path.GetFileName(this.extensionFileName);
            var xpiTarget = Path.Combine(profileDirectory, Path.Combine("extensions", targetFile));
            if (File.Exists(xpiTarget)) File.Delete(xpiTarget);

            string extensionDirectory = Path.Combine(profileDirectory, "extensions");
            if (!Directory.Exists(extensionDirectory))
            {
                Directory.CreateDirectory(extensionDirectory);
            }

            try
            {
                File.Copy(this.extensionFileName, Path.Combine(extensionDirectory, targetFile));
            }
            catch { }
}

@AndreSteenbergen
Copy link
Author

Even better ...... this works without changing code, I am installing "I Don't Care About Cookies" xpi file. Just prepare a directory with the extension already present.

public static IWebDriver GetFirefoxDriver()
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

            var exePath = Assembly.GetExecutingAssembly().Location;
            var directory = Path.GetDirectoryName(exePath);

            Console.WriteLine("Trying to start Firefox in directory:" + directory);

            FirefoxOptions ffOptions = new FirefoxOptions();

            var profileDirecotory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
            Directory.CreateDirectory(Path.Combine(profileDirecotory, "extensions"));
            File.Copy(Path.Combine(directory, "jid1-KKzOGWgsW3Ao4Q@jetpack.xpi"), Path.Combine(profileDirecotory, "extensions", "jid1-KKzOGWgsW3Ao4Q@jetpack.xpi"));

            FirefoxProfile profile = new FirefoxProfile(profileDirecotory, true);

            //profile.AddExtension(Path.Combine(directory, "jid1-KKzOGWgsW3Ao4Q@jetpack.xpi"));

            profile.SetPreference("browser.privacy.trackingprotection.menu", "always");
            profile.SetPreference("privacy.trackingprotection.enabled", true);
            profile.SetPreference("network.cookie.cookieBehavior", 1);
            profile.SetPreference("privacy.donottrackheader.enabled", true);
            profile.SetPreference("privacy.trackingprotection.introCount", 20);
            
            ffOptions.Profile = profile;
            ffOptions.AcceptInsecureCertificates = true;
          //  profile.WriteToDisk();

            //ffOptions.AddArgument("-headless");

#if DEBUG
            FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(directory);
#else
            FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(directory, "geckodriver.sh");
#endif
            var driver = new FirefoxDriver(service, ffOptions);
            var padding = (ReadOnlyCollection<object>)driver.ExecuteScript("return [window.outerWidth-window.innerWidth, window.outerHeight-window.innerHeight];");
            driver.Manage().Window.Size = new Size(1366 + int.Parse(padding[0].ToString()), 768 + int.Parse(padding[0].ToString()));

            return driver;
        }

@barancev
Copy link
Member

As noted in #6929
The c# driver requires the same change as Java #6403.

@miksch123
Copy link

miksch123 commented Sep 15, 2021

as a temporary workaround run following c# method to add a debugging extension. This only works if a webdriver session for firefox has been created.

public static void AddDebugFFExtensionWin(string extensiondir)
        {
            driver.Navigate().GoToUrl("about:debugging#/runtime/this-firefox");
            WebDriverWaiter(time).Until<IWebElement>(x => driver.FindElement(By.XPath("/html/body/div/div/main/article/section[1]/div/section/div/button")));
            DriverCommands.Driver.FindElement(By.XPath("/html/body/div/div/main/article/section[1]/div/section/div/button")).Click();
            Thread.Sleep(1000);

            IUIAutomation automation = new CUIAutomation();
            IUIAutomationCondition cond1 = automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_EditControlTypeId);
            IUIAutomationCondition cond2 = automation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "File name:");
            IUIAutomationAndCondition cond = (IUIAutomationAndCondition)automation.CreateAndCondition(cond1, cond2);
            IUIAutomationElement WindowsElement = UIAutomationWaiter(TimeSpan.FromSeconds(time), TreeScope.TreeScope_Descendants, cond);
            element.SetFocus();
            IUIAutomationValuePattern pattern = (IUIAutomationValuePattern)WindowsElement.GetCurrentPattern(UIA_PatternIds.UIA_ValuePatternId);
            pattern.SetValue(extensiondir);
            System.Drawing.Size openbutton = new System.Drawing.Size(88, 26);
            IUIAutomationElementArray elements = DriverCommands.automation.GetRootElement()
                .FindAll(TreeScope.TreeScope_Descendants, DriverCommands.automation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Open"));
            for (int i = 1; i <= elements.Length; i++)
            {
                IUIAutomationElement elementbutton = ElementCommands.WindowsElements.GetElement(i);
                int left = element.CurrentBoundingRectangle.left;
                int top = element.CurrentBoundingRectangle.top;
                int right = element.CurrentBoundingRectangle.right;
                int bottom = element.CurrentBoundingRectangle.bottom;
                int width = right - left;
                int height = bottom - top;
                System.Drawing.Size actual = new System.Drawing.Size(width, height);
                if (actual == openbutton)
                {
                    IUIAutomationInvokePattern invoke = element.GetCurrentPattern(UIA_PatternIds.UIA_InvokePatternId) as IUIAutomationInvokePattern;
                    invoke.Invoke();
                    return;
                }
            }
        }
public static IUIAutomationElement UIAutomationWaiter(TimeSpan time, TreeScope treeScope, IUIAutomationAndCondition condition)
        {
            IUIAutomationElement element = null;
            TimeSpan currenttime = new TimeSpan(0, 0, 0, 0, 0);

            while (element  == null)
            {
                if (currenttime > time)
                    throw new TimeoutException("UIAutomationWaiter for the element searched using" + treeScope + " and " + condition + " timed out.");

                try
                {
                    element  = automation.GetRootElement().FindFirst(treeScope, condition);
                }
                catch
                {
                }
                currenttime = currenttime.Add(TimeSpan.FromMilliseconds(250));
            }
            return element;
        }

The code displayed should only be something to guide you. You obviously would need to fit it to your project's structure. But this is how I manage to add my extension since the version where I was no longer able to (FF 48). It still runs well to this day.

@titusfortner
Copy link
Member

Further, we recommend using the driver method after starting the session, rather than working with profile.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked and limited conversation to collaborators Jan 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants