protected virtual void ConfigureTimeout(BrowserSettings settings) { this._webDriver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(settings.PageLoadTimeout); //TODO: This setting does not work. Script timeout should be configured via browser-specific settings. this._webDriver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(settings.CommandTimeout); //SetScriptTimeout(TimeSpan.FromSeconds(settings.CommandTimeout)); // old Console.WriteLine("Driver settings: page load timeout {0}, ScriptTimeout {1}.", settings.PageLoadTimeout, settings.CommandTimeout); }
public IBrowserSession Create(BrowserSettings settings) { switch (settings.BrowserType) { case BrowserType.Firefox: return(CreateFirefoxAdapter(settings)); case BrowserType.Chrome: return(CreateChromeAdapter(settings)); default: throw new ArgumentException("Unknown browser type."); } }
static private IBrowserSession CreateFirefoxAdapter(BrowserSettings settings) { string pathToCurrentUserProfiles = Environment.ExpandEnvironmentVariables("%TEMP%") + @"\ff_selenium_etsy3"; //string[] pathsToProfiles = Directory.GetDirectories(pathToCurrentUserProfiles, "*.default", SearchOption.TopDirectoryOnly); //if (pathsToProfiles.Length != 0) //{ // FirefoxProfile profile = new FirefoxProfile(pathsToProfiles[0]); // profile.SetPreference("browser.tabs.loadInBackground", false); // set preferences you need // driver = new FirefoxDriver(new FirefoxBinary(), profile, serverTimeout); //} //FirefoxProfile profile = new FirefoxProfile(@"C:\Users\stefan\AppData\Roaming\Mozilla\Firefox\Profiles\ui906pgj.selenium_etsy", false); //"P:\\var\\ff_profile" //var profileManager = new FirefoxProfileManager(); //FirefoxProfile profile = profileManager.GetProfile(@"ff_etsy"); FirefoxProfile profile = new FirefoxProfile(); if (profile == null) { profile = new FirefoxProfile(); } profile.SetPreference("browser.tabs.loadInBackground", false); profile.SetPreference("dom.max_chrome_script_run_time", settings.CommandTimeout); profile.SetPreference("dom.max_script_run_time", settings.CommandTimeout); //profile.SetPreference("webdriver.firefox.profile", "ff_etsy"); profile.DeleteAfterUse = false; //profile.SetPreference("xpinstall.signatures.required", false); if (settings.PluginFileName != null) { profile.AddExtension(settings.PluginFileName); } var cc = TimeSpan.FromSeconds(settings.CommandTimeout); FirefoxOptions opt = new FirefoxOptions(); //opt.Profile = profile; // To have geckodriver pick up an existing profile on the filesystem, you may pass ["-profile", "/path/to/profile"]. opt.AddArguments(new string[] { "-profile", pathToCurrentUserProfiles }); // @"P:\var\ff_profile" //opt.AddArguments( new string[] { "-P", "ff_etsy" }); //opt.AddAdditionalCapability(CapabilityType.AcceptSslCertificates, true); //opt.AddAdditionalCapability(CapabilityType.IsJavaScriptEnabled, true); //opt.AddAdditionalCapability(CapabilityType.HasNativeEvents, true); //opt.SetPreference("webdriver.firefox.profile", "ff_etsy"); //var webDriver = new FirefoxDriver(FirefoxDriverService.CreateDefaultService(), opt, TimeSpan.FromSeconds(settings.CommandTimeout)); var webDriver = new FirefoxDriver(opt); return(new DefaultBrowserSession(webDriver).Configure(settings)); }
static private IBrowserSession CreateChromeAdapter(BrowserSettings settings) { var options = new ChromeOptions(); options.AddArgument("window-size=1280,768"); //options.AddArgument("disable-popup-blocking"); options.AddArgument("disable-3d-apis"); options.AddArgument("disable-gpu"); // chrome_options.add_argument("--profile-directory=Profile1") string pathToCurrentUserProfiles = Environment.ExpandEnvironmentVariables("%TEMP%") + @"\chrome_selenium_etsy"; // + indx.ToString(); indx++; options.AddArgument("user-data-dir=" + pathToCurrentUserProfiles); if (settings.PluginFileName != null) { options.AddExtension(settings.PluginFileName); } options.AddArgument("sidfordelete=" + new Random().Next()); var webDriver = CustomChromeDriver.Build(options, TimeSpan.FromSeconds(settings.CommandTimeout)); return(new ChromeBrowserSession(webDriver).Configure(settings)); }
//public PageContent Scrape(Uri uri, bool isReferenceScraping = true) //{ // DateTime startedAt = DateTime.Now; // try // { // Thread.Sleep(100); // this._webDriver.Url = uri.ToString(); // // wait page fully load // Thread.Sleep(1000); // } // catch (WebDriverTimeoutException) // { // // Assuming is was enough time to load a page. Page is scraped "as is". // } // _tracer.TraceEvent(TraceEventType.Verbose, 0, "Page {0} opened in {1:f3} seconds.", uri, // (DateTime.Now - startedAt).TotalSeconds); // this.InjectHelperScripts(); // PageContent content = this.ScrapeContent(isReferenceScraping); // _tracer.TraceEvent(TraceEventType.Verbose, 0, "Page content has been scaped."); // Image screenshot = this.GetScreenshot(); // _tracer.TraceEvent(TraceEventType.Verbose, 0, "Screenshot image has taken."); // content.Screenshot = screenshot; // return content; //} #endregion public DefaultBrowserSession Configure(BrowserSettings settings) { this.ConfigureWindowSize(settings); this.ConfigureTimeout(settings); return(this); }
protected virtual void ConfigureWindowSize(BrowserSettings settings) { this._webDriver.Manage().Window.Size = settings.WindowSize; }
protected override void ConfigureWindowSize(BrowserSettings settings) { // Window size is configured in Chrome options. base.ConfigureWindowSize(settings); }