2017-04-06 9 views
6

मैं सेलेनियम 2 से सेलेनियम 3 लेकिन पुराने हैंडलिंग, जो बहुत आसान और तेजी से अब और काम नहीं करता था (और प्रलेखन के रूप में ऐसा लगता है nonexisting है)सेलेनियम के साथ एक विशिष्ट फ़ायरफ़ॉक्स प्रोफ़ाइल शुरू 3

को उन्नत करने के लिए कोशिश कर रहा हूँ

इस पल में कार्यक्रम है और क्या मैं चाहता हूँ प्रोफ़ाइल के साथ एक 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(); 
    } 

प्रोफ़ाइल लोड किए बिना यह केवल नए फ़ायरफ़ॉक्स ड्रायवर (_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 लाइब्रेरी में एक बग के कारण है। प्रोफाइल का ज़िप उत्पन्न करने वाला कोड उचित ज़िप प्रदान करने में विफल रहा है।

एक तरह से इस समस्या को दूर करने के लिए दोषपूर्ण ZipStorer के बजाय FirefoxOptions ओवरलोड और नेट फ्रेमवर्क (System.IO.Compression.ZipArchive) से archiver उपयोग करने के लिए होगा:

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

धन्यवाद, मैं वास्तव में सेलेनियम 3 का उपयोग करने में सक्षम होने पर छोड़ रहा था ... ऐसा एक - मैं कहूंगा, लोलेवल बेसिक - जो काम नहीं करता वास्तव में दर्द होता है ... और इस समस्या को कहीं भी नहीं ढूंढना कहीं और भी है। अधिभारित संस्करण के साथ मुझे अब SELENIUM प्रोफ़ाइल का पथ पता होना चाहिए, जो वास्तव में मेरे लिए काम नहीं कर रहा है, क्योंकि हमारे पास प्रोफ़ाइल के साथ कई अलग-अलग परीक्षण कंप्यूटर हैं, लेकिन अन्य स्थानों पर भी। (नया फ़ायरफ़ॉक्सप्रोफाइल मैनेजर())। GetProfile ("SELENIUM")। प्रोफ़ाइल डायरेक्टरी जब मैं इसे कोशिश करता हूं तो प्रोफ़ाइल डायरेक्टरी हमेशा एक गैर सार्वजनिक चर –

+0

@ डोमिनिक लेम्बर्गर में संग्रहीत होती है, प्रत्येक प्रोफ़ाइल के पथ को रिफ्लेक्सन द्वारा एक्सेस किया जा सकता है 'फ़ायरफ़ॉक्सप्रोफाइल प्रबंधक 'का एक उदाहरण:' var प्रोफाइल = (शब्दकोश <स्ट्रिंग, स्ट्रिंग>) प्रबंधक। गेटटाइप()। गेटफिल्ड (" प्रोफाइल ", बाइंडिंगफ्लैग्स। इंस्टेंस | बाइंडिंगफ्लैग .ऑनपब्लिक) .GetValue (प्रबंधक);'। –

+0

यह मेरे मामले में कैसा लगेगा? फ़ायरफ़ॉक्स प्रोफाइल प्रोफ़ाइल = नया फ़ायरफ़ॉक्सप्रोफाइल प्रबंधक()। GetProfile ("SELENIUM"); var प्रोफाइल = (शब्दकोश ) profile.GetType()। GetField ("ProfileDirectory", बाइंडिंगफ्लैग्स। इंस्टेंस | बाइंडिंगफ्लैग .ऑनपब्लिक) .GetValue (प्रोफाइल); विकल्प। प्रोफाइल = कैंट इसे काम करने के लिए प्रतीत होता है (पीएस: पहले कभी इस्तेमाल किया गया प्रतिबिंब) –

संबंधित मुद्दे