2017-04-06 64 views
6

我正在嘗試從Selenium 2升級到Selenium 3,但舊的處理方式非常簡單且快速無法工作(並且文檔看起來不存在)用Selenium 3啓動特定的Firefox配置文件

這是程序的時刻,我要的是打開Firefox驅動程序配置文件:硒

可悲的是它不工作,始終關閉與錯誤:

An unhandled exception of type 'System.InvalidOperationException' occurred in WebDriver.dll 

Additional information: corrupt deflate stream 

這是我目前的計劃:

public Program() 
    { 
     FirefoxOptions _options = new FirefoxOptions(); 
     FirefoxProfileManager _profileIni = new FirefoxProfileManager(); 
     FirefoxDriverService _service = FirefoxDriverService.CreateDefaultService(@"C:\Programme\IMaT\Output\Release\Bin"); 
     _service.FirefoxBinaryPath = @"C:\Program Files (x86)\Mozilla Firefox\firefox.exe"; 
     try 
     { 
      if ((_options.Profile = _profileIni.GetProfile("SELENIUM")) == null) 
      { 
       Console.WriteLine("SELENIUM PROFILE NOT FOUND"); 
       _profile.SetPreference("network.proxy.type", 0); // disable proxy 
       _profile = new FirefoxProfile(); 
      } 
     } 
     catch 
     { 
      throw new Exception("Firefox needs a Profile with \"SELENIUM\""); 
     } 
     IWebDriver driver = new FirefoxDriver(_service,_options,new System.TimeSpan(0,0,30));   
     driver.Navigate().GoToUrl("ld-hybrid.fronius.com"); 
     Console.Write("rtest"); 
    } 

    static void Main(string[] args) 
    { 
     new Program(); 
    } 

不加載配置文件它只適用於新的FirefoxDriver(_service),但配置文件是強制性的。

硒2我使用此代碼來處理它:

FirefoxProfileManager _profileIni = new FirefoxProfileManager(); 
     // use custom temporary profile 

     try { if ((_profile = _profileIni.GetProfile("SELENIUM")) == null) 
      { 
       Console.WriteLine("SELENIUM PROFILE NOT FOUND"); 
       _profile.SetPreference("network.proxy.type", 0); // disable proxy 
       _profile = new FirefoxProfile(); 
      } 
     } 
     catch 
     { 
      throw new Exception("Firefox needs a Profile with \"SELENIUM\""); 
     } 

     _profile.SetPreference("intl.accept_languages", _languageConfig); 

     _driver = new FirefoxDriver(_profile); 

快速,簡單,但由於驅動程序不支持與服務構造和配置文件我真的不知道如何得到這個工作,任何幫助,將不勝感激

回答

4

此例外是由於.Net庫中的錯誤。生成配置文件Zip的代碼無法提供適當的Zip。爲了解決這個問題

一種方法是超載FirefoxOptions並使用NET框架(System.IO.Compression.ZipArchive)歸檔,而不是錯誤的ZipStorer

var options = new FirefoxOptionsEx(); 
options.Profile = @"C:\Users\...\AppData\Roaming\Mozilla\Firefox\Profiles\ez3krw80.Selenium"; 
options.SetPreference("network.proxy.type", 0); 

var service = FirefoxDriverService.CreateDefaultService(@"C:\downloads", "geckodriver.exe"); 

var driver = new FirefoxDriver(service, options, TimeSpan.FromMinutes(1)); 
class FirefoxOptionsEx : FirefoxOptions { 

    public new string Profile { get; set; } 

    public override ICapabilities ToCapabilities() { 

     var capabilities = (DesiredCapabilities)base.ToCapabilities(); 
     var options = (IDictionary)capabilities.GetCapability("moz:firefoxOptions"); 
     var mstream = new MemoryStream(); 

     using (var archive = new ZipArchive(mstream, ZipArchiveMode.Create, true)) { 
      foreach (string file in Directory.EnumerateFiles(Profile, "*", SearchOption.AllDirectories)) { 
       string name = file.Substring(Profile.Length + 1).Replace('\\', '/'); 
       if (name != "parent.lock") { 
        using (Stream src = File.OpenRead(file), dest = archive.CreateEntry(name).Open()) 
         src.CopyTo(dest); 
       } 
      } 
     } 

     options["profile"] = Convert.ToBase64String(mstream.GetBuffer(), 0, (int)mstream.Length); 

     return capabilities; 
    } 

} 

並且按名稱獲取配置文件的目錄:

var manager = new FirefoxProfileManager(); 
var profiles = (Dictionary<string, string>)manager.GetType() 
    .GetField("profiles", BindingFlags.Instance | BindingFlags.NonPublic) 
    .GetValue(manager); 

string directory; 
if (profiles.TryGetValue("Selenium", out directory)) 
    options.Profile = directory; 
+0

謝謝,我真的放棄了能夠使用Selenium 3 ...這樣的 - 我會說,Lowlevel Basic - 那不起作用真的很痛苦...並且任何地方對這個問題沒有任何發現甚至更多。 由於我們有多個不同的測試計算機與配置文件,但在其他位置等,我需要知道SELENIUM配置文件的路徑現在,這不是真的爲我工作的重載版本。我試過這個ProfileDirectory總是空的,它只存儲在一個非公共變量中 –

+0

@Dominik Lemberger,每個配置文件的路徑可以通過反射訪問( )(新的FirefoxProfileManager())。GetProfile(「SELENIUM」一個'FirefoxProfileManager'實例:'var profiles =(Dictionary )manager.GetType()。GetField(「profiles」,BindingFlags.Instance | BindingFlags.NonPublic).GetValue(manager);'。 –

+0

這在我的情況下會怎樣? FirefoxProfile配置文件=新的FirefoxProfileManager()。GetProfile(「SELENIUM」); var profile =(Dictionary )profile.GetType()。GetField(「ProfileDirectory」,BindingFlags.Instance | BindingFlags.NonPublic).GetValue(profile); options.profile = 不能使它工作(ps:以前沒有使用反射) –

相關問題