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

Exception thrown when instantiating FirefoxDriver on C# .NET #4816

Closed
cltsang opened this issue Oct 6, 2017 · 11 comments
Closed

Exception thrown when instantiating FirefoxDriver on C# .NET #4816

cltsang opened this issue Oct 6, 2017 · 11 comments
Labels

Comments

@cltsang
Copy link

cltsang commented Oct 6, 2017

Meta -

OS: Windows 10 and Mac OS

Selenium Version: WebDriver 3.6.0, C#

Browser: Tor Browser

Browser Version: 7.0.6 (based on Firefox 52.4.0 (64-Bit))

Expected Behavior -

According to the Nuget page, the WebDriver supports .Net Standard 2.0.

Actual Behavior -

when running on a .NET Core 2.0 console project, I encountered an exception
System.TypeInitializationException: The type initializer for 'System.IO.Compression.ZipStorer' threw an exception. ---> System.NotSupportedException: No data is available for encoding 437.

Steps to reproduce -

Just start a new project and instantiate FirefoxDriver by

var torBinaryPath = @"/Applications/TorBrowser.app/Contents/MacOS/firefox";
var process = new Process();
process.StartInfo.FileName = torBinaryPath;
process.StartInfo.Arguments = "-n";
process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
process.Start();
var profile = new FirefoxProfile();
profile.SetPreference("network.proxy.type", 1);
profile.SetPreference("network.proxy.socks", "127.0.0.1");
profile.SetPreference("network.proxy.socks_port", 9150);
var driver = new FirefoxDriver(profile);

Am I doing anything incorrectly here?
Please advise, thank you.

@p0deje
Copy link
Member

p0deje commented Oct 6, 2017

Can you reproduce it with normal Firefox, not TorBrowser?

@p0deje p0deje added the C-dotnet label Oct 6, 2017
@cltsang
Copy link
Author

cltsang commented Oct 6, 2017

No, unfortunately. So I'm not even sure where I should start looking.

@barancev
Copy link
Member

barancev commented Oct 6, 2017

Tor Browser is not supported at present, watch #4239

@p0deje
Copy link
Member

p0deje commented Oct 6, 2017

I'm going to close this for now since there might tons of issues with Tor Browser + Selenium, but this is fine as long as it's not supported.

Feel free to jump in the conversation in #4239.

@p0deje p0deje closed this as completed Oct 6, 2017
@LHCGreg
Copy link

LHCGreg commented Dec 5, 2017

@p0deje Can this be reopened? This is not a Tor browser issue. I can reproduce with regular Firefox, running on .net core 2.0 on Windows.

Reproduction code:

using System;
using OpenQA.Selenium.Firefox;

namespace FirefoxTest
{
    class Program
    {
        static void Main(string[] args)
        {
            const string geckoDriverDirectory = @"C:\Program Files\GeckoDriver";

            FirefoxOptions firefoxOptions = new FirefoxOptions()
            {
                Profile = new FirefoxProfile()
                {
                    DeleteAfterUse = true,
                },
            };
            firefoxOptions.SetPreference("javascript.enabled", false);

            using (FirefoxDriver driver = new FirefoxDriver(geckoDriverDirectory, firefoxOptions))
            {
                driver.Quit();
            }
        }
    }
}

Error:

Unhandled Exception: System.TypeInitializationException: The type initializer for 'System.IO.Compression.ZipStorer' threw an exception. ---> System.NotSupportedException: No data is available for encoding 437. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.
   at System.Text.Encoding.GetEncoding(Int32 codepage)
   at System.IO.Compression.ZipStorer..cctor()
   --- End of inner exception stack trace ---
   at System.IO.Compression.ZipStorer.WriteLocalHeader(ZipFileEntry& zipFileEntry)
   at System.IO.Compression.ZipStorer.AddStream(CompressionMethod compressionMethod, Stream sourceStream, String fileNameInZip, DateTime modificationTimeStamp, String fileEntryComment)
   at System.IO.Compression.ZipStorer.AddFile(CompressionMethod compressionMethod, String sourceFile, String fileNameInZip, String fileEntryComment)
   at OpenQA.Selenium.Firefox.FirefoxProfile.ToBase64String()
   at OpenQA.Selenium.Firefox.FirefoxOptions.GenerateFirefoxOptionsDictionary()
   at OpenQA.Selenium.Firefox.FirefoxOptions.ToCapabilities()
   at OpenQA.Selenium.Firefox.FirefoxDriver.ConvertOptionsToCapabilities(FirefoxOptions options)
   at OpenQA.Selenium.Firefox.FirefoxDriver..ctor(String geckoDriverDirectory, FirefoxOptions options)
   at FirefoxTest.Program.Main(String[] args) in D:\documents\visual studio 2017\Projects\FirefoxTest\FirefoxTest\Program.cs:line 21

Looks like the ZipStorer code needs encoding 437. .NET core removes built-in support for many less common encodings, including 437. To make the encoding available in .NET core, you need to reference the System.Text.Encoding System.Text.Encoding.CodePages Nuget package and somewhere before Encoding.GetEncoding(437) gets called, make sure to call Encoding.RegisterProvider(CodePagesEncodingProvider.Instance).

As a workaround users can do the above steps in their own code.

LHCGreg added a commit to LHCGreg/animerecs that referenced this issue Dec 5, 2017
@barancev barancev reopened this Dec 5, 2017
@stewart-r
Copy link

Thanks very much for the workaround @LHCGreg - although I think CodePagesEncodingProvider lives in the System.Text.Encoding.CodePages nuget package rather than System.Text.Encoding

@LHCGreg
Copy link

LHCGreg commented Dec 7, 2017

Yes, sorry, I got the link correct but the name wrong.

@adambeben
Copy link

adambeben commented Mar 26, 2019

Same exception is thrown when uploading file with RemoteWebDriver, eg:

if (_driver.GetType() == typeof(RemoteWebDriver))
{
	((RemoteWebDriver)_driver).FileDetector = new LocalFileDetector();
}

FileInput.SendKeys(filePath);

@LHCGreg workaround works here also. Thanks a lot!

@olegbaslak
Copy link

Also confirmed workaround works for netcore2.2 + chromedvier

@jimevans
Copy link
Member

I don’t want to lock discussion on this issue. The ZipStorer code has been removed entirely from the code base for the 4.0 development cycle. Users should at least give the 4.0 alpha 1 before commenting.

@diemol
Copy link
Member

diemol commented Sep 11, 2020

As the most recent comment says, please use the referenced version and keep an eye on the upcoming releases.

Will close this issue since it has been fixed on trunk

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

9 participants