/// <summary> /// Simulates a click on an element, which has differing effects depending on the element type. If the element /// is a BUTTON or INPUT TYPE=SUBMIT or INPUT TYPE=IMAGE element, the current form (if any) will be submitted, /// with the name/value of the clicked element being used in the submission values where relevant. If the /// element is a checkbox, the CHECKED value will be toggled on or off. If the element is a radio button, other /// radio buttons in the group will have their CHECKED attribute removed and the current element will have its /// CHECKED element set. /// </summary> public ClickResult Click() { AssertElementExists(); AssertElementIsNotDisabled(); _browser.Log("Clicking element: " + HttpUtility.HtmlEncode(XElement.ToString()), LogMessageType.Internal); return(_current.Click()); }
/// <summary> /// Initializes a new instance of the <see cref="HtmlResult"/> class. /// </summary> /// <param name="results">A collection of <see cref="HtmlElement"/></param> /// <param name="browser">The browser instance where the HTML results were found</param> internal HtmlResult(List <HtmlElement> results, Browser browser) { this.currentElement = results.Count > 0 ? results[0] : null; this.resultList = results; this.browser = browser; browser.Log("New HTML result set obtained, containing " + results.Count + " element(s)", LogMessageType.Internal); }
static bool LastRequestFailed(Browser browser) { if(browser.LastWebException != null) { browser.Log("There was an error loading the page: " + browser.LastWebException.Message); return true; } return false; }
static void Main(string[] args) { var browser = new Browser(); try { // log the browser request/response data to files so we can interrogate them in case of an issue with our scraping browser.RequestLogged += OnBrowserRequestLogged; browser.MessageLogged += new Action<Browser, string>(OnBrowserMessageLogged); // we'll fake the user agent for websites that alter their content for unrecognised browsers browser.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10"; // browse to GitHub browser.Navigate("http://www.pogdesign.co.uk/cat/"); if (LastRequestFailed(browser)) return; // always check the last request in case the page failed to load browser.Log("First we need do find a Title with month and year description."); var titleLink = browser.Find("a", FindBy.Text, "August 2016 TV Episode Calendar"); if (!titleLink.Exists) { browser.Log("Can't find the link! Perhaps the site is down for maintenance?"); return; } browser.Log("Then we find the yesterday column."); // Obter dia var dayDiv = browser.Find("div", FindBy.Id, "d_1_8_2016"); if (!dayDiv.Exists) { browser.Log("Can't find the column! Perhaps dont have any series for the month."); return; } var serieLink = browser.Find("a href", FindBy.PartialText, "-summary"); if (serieLink.Exists) { Console.WriteLine(serieLink.TotalElementsFound); Console.WriteLine(serieLink.FirstOrDefault().Value); } //// click the login link and click it //browser.Log("First we need to log in, so browse to the login page, fill in the login details and submit the form."); //var loginLink = browser.Find("a", FindBy.Text, "Login"); //if (!loginLink.Exists) // browser.Log("Can't find the login link! Perhaps the site is down for maintenance?"); //else //{ // loginLink.Click(); // if (LastRequestFailed(browser)) return; // // fill in the form and click the login button - the fields are easy to locate because they have ID attributes // browser.Find("login_field").Value = "*****@*****.**"; // browser.Find("password").Value = "yourpassword"; // browser.Find(ElementType.Button, "name", "commit").Click(); // if (LastRequestFailed(browser)) return; // // see if the login succeeded - ContainsText() is very forgiving, so don't worry about whitespace, casing, html tags separating the text, etc. // if (browser.ContainsText("Incorrect login or password")) // { // browser.Log("Login failed!", LogMessageType.Error); // } // else // { // // After logging in, we should check that the page contains elements that we recognise // if (!browser.ContainsText("Your Repositories")) // browser.Log("There wasn't the usual login failure message, but the text we normally expect isn't present on the page"); // else // { // browser.Log("Your News Feed:"); // // we can use simple jquery selectors, though advanced selectors are yet to be implemented // foreach (var item in browser.Select("div.news .title")) // browser.Log("* " + item.Value); // } // } //} } catch (Exception ex) { browser.Log(ex.Message, LogMessageType.Error); browser.Log(ex.StackTrace, LogMessageType.StackTrace); } finally { var path = WriteFile("log-" + DateTime.UtcNow.Ticks + ".html", browser.RenderHtmlLogFile("SimpleBrowser Sample - Request Log")); //Process.Start(path); } Console.ReadKey(); }
static void Main(string[] args) { var browser = new Browser(); try { // log the browser request/response data to files so we can interrogate them in case of an issue with our scraping browser.RequestLogged += OnBrowserRequestLogged; browser.MessageLogged += new Action<Browser, string>(OnBrowserMessageLogged); // we'll fake the user agent for websites that alter their content for unrecognised browsers browser.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10"; // browse to GitHub browser.Navigate("http://github.com/"); if(LastRequestFailed(browser)) return; // always check the last request in case the page failed to load // click the login link and click it browser.Log("First we need to log in, so browse to the login page, fill in the login details and submit the form."); var loginLink = browser.Find("a", FindBy.Text, "Login"); if(!loginLink.Exists) browser.Log("Can't find the login link! Perhaps the site is down for maintenance?"); else { loginLink.Click(); if(LastRequestFailed(browser)) return; // fill in the form and click the login button - the fields are easy to locate because they have ID attributes browser.Find("login_field").Value = "*****@*****.**"; browser.Find("password").Value = "yourpassword"; browser.Find(ElementType.Button, "name", "commit").Click(); if(LastRequestFailed(browser)) return; // see if the login succeeded - ContainsText() is very forgiving, so don't worry about whitespace, casing, html tags separating the text, etc. if(browser.ContainsText("Incorrect login or password")) { browser.Log("Login failed!", LogMessageType.Error); } else { // After logging in, we should check that the page contains elements that we recognise if(!browser.ContainsText("Your Repositories")) browser.Log("There wasn't the usual login failure message, but the text we normally expect isn't present on the page"); else { browser.Log("Your News Feed:"); // we can use simple jquery selectors, though advanced selectors are yet to be implemented foreach(var item in browser.Select("div.news .title")) browser.Log("* " + item.Value); } } } } catch(Exception ex) { browser.Log(ex.Message, LogMessageType.Error); browser.Log(ex.StackTrace, LogMessageType.StackTrace); } finally { var path = WriteFile("log-" + DateTime.UtcNow.Ticks + ".html", browser.RenderHtmlLogFile("SimpleBrowser Sample - Request Log")); Process.Start(path); } }