SeleniumHQ / selenium

A browser automation framework and ecosystem.
https://selenium.dev
Apache License 2.0
30.8k stars 8.21k forks source link

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

Closed cltsang closed 4 years ago

cltsang commented 7 years ago

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 commented 7 years ago

Can you reproduce it with normal Firefox, not TorBrowser?

cltsang commented 7 years ago

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

barancev commented 7 years ago

Tor Browser is not supported at present, watch #4239

p0deje commented 7 years ago

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.

LHCGreg commented 6 years ago

@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.

stewart-r commented 6 years ago

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 commented 6 years ago

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

adambeben commented 5 years ago

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 commented 5 years ago

Also confirmed workaround works for netcore2.2 + chromedvier

jimevans commented 5 years ago

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 commented 4 years ago

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