Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Diagnostics;
- using System.Drawing;
- using System.IO;
- using System.Net;
- using Newtonsoft.Json.Linq;
- using OpenQA.Selenium;
- using OpenQA.Selenium.Chrome;
- using OpenQA.Selenium.Support.Extensions;
- using System.Text.RegularExpressions;
- namespace Selenium
- {
- public class Browser
- {
- public string name = string.Empty;
- public string anddress = string.Empty;
- public string username = string.Empty;
- public string password = string.Empty;
- public string phone = string.Empty;
- public List<string> EmailList = new List<string>();
- public List<string> LinksList = new List<string>();
- public IWebDriver driver = null;
- public void Start()
- {
- driver = new ChromeDriver();
- }
- public IWebElement GetElement(string type, string name)
- {
- switch (type.ToUpper())
- {
- case "ID":
- return driver.FindElement(By.Id(name));
- break;
- case "XPATH":
- return driver.FindElement(By.XPath(name));
- break;
- case "CLASS":
- return driver.FindElement(By.ClassName(name));
- break;
- case "CSS":
- return driver.FindElement(By.CssSelector(name));
- break;
- case "TAGNAME":
- return driver.FindElement(By.TagName(name));
- break;
- case "LINKTEXT":
- return driver.FindElement(By.LinkText(name));
- default:
- return null;
- }
- }
- public string GoogleSearchGoNext()
- {
- try
- {
- Click(GetElement("xpath", "//*[@id='pnnext']"));
- return "200";
- }
- catch (Exception)
- {
- return "404";
- }
- }
- public string GoogleSeachGoBack()
- {
- try
- {
- Click(GetElement("xpath", "//*[@id='pnprev']"));
- return "200";
- }
- catch (Exception)
- {
- return "404";
- }
- }
- public void Exit()
- {
- driver.Quit();
- }
- public void CheckBox(IWebElement element)
- {
- element.Click();
- }
- public void Click(IWebElement element)
- {
- element.Click();
- }
- public void Fill(IWebElement element, string text)
- {
- element.SendKeys(text);
- }
- public void Wait(int delay)
- {
- Stopwatch sw = new Stopwatch();
- sw.Start();
- while (true)
- {
- if (sw.Elapsed.Milliseconds >= delay) break;
- }
- sw.Reset();
- }
- public void Navigate(string url)
- {
- driver.Navigate().GoToUrl(url);
- }
- public void ImplicitWait(int delay)
- {
- driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(delay);
- }
- public void ToFrame(IWebElement element)
- {
- driver.SwitchTo().Frame(element);
- }
- public void DefaultContent()
- {
- driver.SwitchTo().DefaultContent();
- }
- public object InjectJs(string code)
- {
- return ((IJavaScriptExecutor)driver).ExecuteScript(code);
- }
- public void Hide()
- {
- driver.Manage().Window.Position = new Point(-3000, 0);
- }
- public void Show()
- {
- driver.Manage().Window.Position = new Point(0, 0);
- }
- public void ScreenShot(string output)
- {
- Image img = GetEntireScreenshot();
- img.Save(output);
- }
- private Image GetEntireScreenshot()
- {
- // Get the total size of the page
- var totalWidth = (int)(long)((IJavaScriptExecutor)driver).ExecuteScript("return document.body.offsetWidth"); //documentElement.scrollWidth");
- var totalHeight = (int)(long)((IJavaScriptExecutor)driver).ExecuteScript("return document.body.parentNode.scrollHeight");
- // Get the size of the viewport
- var viewportWidth = (int)(long)((IJavaScriptExecutor)driver).ExecuteScript("return document.body.clientWidth"); //documentElement.scrollWidth");
- var viewportHeight = (int)(long)((IJavaScriptExecutor)driver).ExecuteScript("return window.innerHeight"); //documentElement.scrollWidth");
- // We only care about taking multiple images together if it doesn't already fit
- if (totalWidth <= viewportWidth && totalHeight <= viewportHeight)
- {
- var screenshot = driver.TakeScreenshot();
- return ScreenshotToImage(screenshot);
- }
- // Split the screen in multiple Rectangles
- var rectangles = new List<Rectangle>();
- // Loop until the totalHeight is reached
- for (var y = 0; y < totalHeight; y += viewportHeight)
- {
- var newHeight = viewportHeight;
- // Fix if the height of the element is too big
- if (y + viewportHeight > totalHeight)
- {
- newHeight = totalHeight - y;
- }
- // Loop until the totalWidth is reached
- for (var x = 0; x < totalWidth; x += viewportWidth)
- {
- var newWidth = viewportWidth;
- // Fix if the Width of the Element is too big
- if (x + viewportWidth > totalWidth)
- {
- newWidth = totalWidth - x;
- }
- // Create and add the Rectangle
- var currRect = new Rectangle(x, y, newWidth, newHeight);
- rectangles.Add(currRect);
- }
- }
- // Build the Image
- var stitchedImage = new Bitmap(totalWidth, totalHeight);
- // Get all Screenshots and stitch them together
- var previous = Rectangle.Empty;
- foreach (var rectangle in rectangles)
- {
- // Calculate the scrolling (if needed)
- if (previous != Rectangle.Empty)
- {
- var xDiff = rectangle.Right - previous.Right;
- var yDiff = rectangle.Bottom - previous.Bottom;
- // Scroll
- ((IJavaScriptExecutor)driver).ExecuteScript(String.Format("window.scrollBy({0}, {1})", xDiff, yDiff));
- }
- // Take Screenshot
- var screenshot = driver.TakeScreenshot();
- // Build an Image out of the Screenshot
- var screenshotImage = ScreenshotToImage(screenshot);
- // Calculate the source Rectangle
- var sourceRectangle = new Rectangle(viewportWidth - rectangle.Width, viewportHeight - rectangle.Height, rectangle.Width, rectangle.Height);
- // Copy the Image
- using (var graphics = Graphics.FromImage(stitchedImage))
- {
- graphics.DrawImage(screenshotImage, rectangle, sourceRectangle, GraphicsUnit.Pixel);
- }
- // Set the Previous Rectangle
- previous = rectangle;
- }
- return stitchedImage;
- }
- private Image ScreenshotToImage(Screenshot screenshot)
- {
- Image screenshotImage;
- using (var memStream = new MemoryStream(screenshot.AsByteArray))
- {
- screenshotImage = Image.FromStream(memStream);
- }
- return screenshotImage;
- }
- public void FakePerson()
- {
- WebClient wc = new WebClient();
- ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
- string source = wc.DownloadString("http://api.namefake.com/");
- JObject j = JObject.Parse(source);
- name = j["name"].ToString();
- anddress = j["address"].ToString();
- username = j["username"].ToString();
- password = j["password"].ToString();
- phone = j["phone_h"].ToString();
- }
- public List<string> ScrapEmail()
- {
- var pageSource = driver.PageSource;
- var regex = new Regex(@"[\w\.-]+@[\w\.-]+");
- foreach (Match match in regex.Matches(pageSource))
- {
- EmailList.Add(match.Value);
- }
- return EmailList;
- }
- public List<string> RetrieveAllLinks()
- {
- ReadOnlyCollection<IWebElement> links = driver.FindElements(By.TagName("a"));
- foreach (IWebElement el in links)
- {
- LinksList.Add(el.GetAttribute("href"));
- }
- return LinksList;
- }
- public void ScrollTo(int y)
- {
- InjectJs(string.Format("return window.page{0}Offset;", y));
- }
- }
- }
Add Comment
Please, Sign In to add comment