2017-04-06 49 views
6

Selenium 2'den Selenium 3'e geçmeye çalışıyorum ancak oldukça kolay ve hızlı olan eski işlem artık çalışmıyor (ve belgeler göründüğü gibi yok)Selenium ile belirli bir Firefox Profili Başlatma 3

Bu anda programdır ve ne istediğiniz profili ile bir Firefox sürücüsünü açmaktır:

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

Additional information: corrupt deflate stream 

Bu:

ne yazık çalışmıyor ve her zaman Hata ile kapanır selenyum şu andaki programım:

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(); 
    } 

Profil Yüklemeden, yeni FirefoxDriver (_service) ile çalışır, ancak profil zorunludur.

Selenyum 2'de bu kodla ele:

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); 

Hızlı ve basit ama Sürücü hizmetiyle Oluşturucu desteklemek ve profil olmadığı için ben gerçekten bu nasıl bilmiyorum iş, herhangi bir yardım

cevap

4

Takdir edilecektir. Bunun istisnası .Net kütüphanesindeki bir hatadan kaynaklanmaktadır. Profilin Zip dosyasını oluşturan kod uygun bir Zip sağlamada başarısız oluyor. Bu sorunu aşmak için

bir yolu yerine hatalı ZipStorer ait FirefoxOptions aşırı ve .Net framework (System.IO.Compression.ZipArchive) den arşivleyicisini kullanmak olacaktır:

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; 
:

Ve adında bir profil için dizin almak için

+0

Teşekkürler, Selenium 3'ü kullanabilmekten vazgeçiyordum ... Böyle bir şey - Lowlevel Basic - işe yaramadı, gerçekten bir acı ... ve bu soruna hiçbir şey bulamayan her şey daha da fazla. Aşırı yüklenmiş sürümle, SELENIUM Profilinin Yolunu bilmem gerekiyor, bu benim için gerçekten işe yaramıyor çünkü profille birden çok farklı Test Bilgisayarımız var, ancak diğer yerlerde vb. (yeni FirefoxProfileManager()) GetProfile ("SELENIUM") ProfileDirectory bunu denediğimde ProfileDirectory her zaman boş olan bir public değişkeni içinde saklanan sadece –

+0

@Dominik Lemberger, her profilin yolunu yansıtma ile erişilebilir FirefoxProfileManager'ın bir örneği: var profilleri = (Sözlük ) manager.GetType(). GetField ("profiles", BindingFlags.Instance | BindingFlags.NonPublic) .GetValue (manager); '. –

+0

Bu durum benim durumuma nasıl bakardı? FirefoxProfile profile = yeni FirefoxProfileManager(). GetProfile ("SELENIUM"); var profiles = (Sözlük ) profile.GetType(). GetField ("ProfileDirectory", BindingFlags.Instance | BindingFlags.NonPublic) .GetValue (profile); options.Profile = İşe yaradığı görülüyor (ps: daha önce hiç kullanılmamış reflexion) –

İlgili konular