Advertisement
Guest User

UpdatedCode

a guest
Sep 13th, 2017
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Diagnostics;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Net;
  8. using Newtonsoft.Json.Linq;
  9. using OpenQA.Selenium;
  10. using OpenQA.Selenium.Chrome;
  11. using OpenQA.Selenium.Support.Extensions;
  12. using System.Text.RegularExpressions;
  13.  
  14. namespace Selenium
  15. {
  16.     public class Browser
  17.     {
  18.         public string name = string.Empty;
  19.         public string anddress = string.Empty;
  20.         public string username = string.Empty;
  21.         public string password = string.Empty;
  22.         public string phone = string.Empty;
  23.  
  24.         public List<string> EmailList = new List<string>();
  25.         public List<string> LinksList = new List<string>();
  26.  
  27.         public IWebDriver driver = null;
  28.  
  29.         public void Start()
  30.         {
  31.             driver = new ChromeDriver();
  32.         }
  33.  
  34.         public IWebElement GetElement(string type, string name)
  35.         {
  36.             switch (type.ToUpper())
  37.             {
  38.                 case "ID":
  39.                     return driver.FindElement(By.Id(name));
  40.                     break;
  41.                 case "XPATH":
  42.                     return driver.FindElement(By.XPath(name));
  43.                     break;
  44.                 case "CLASS":
  45.                     return driver.FindElement(By.ClassName(name));
  46.                     break;
  47.                 case "CSS":
  48.                     return driver.FindElement(By.CssSelector(name));
  49.                     break;
  50.                 case "TAGNAME":
  51.                     return driver.FindElement(By.TagName(name));
  52.                     break;
  53.                 case "LINKTEXT":
  54.                     return driver.FindElement(By.LinkText(name));
  55.                     default:
  56.                         return null;
  57.  
  58.             }
  59.         }
  60.  
  61.         public string GoogleSearchGoNext()
  62.         {
  63.             try
  64.             {
  65.                 Click(GetElement("xpath", "//*[@id='pnnext']"));
  66.                 return "200";
  67.             }
  68.             catch (Exception)
  69.             {
  70.                 return "404";
  71.             }
  72.         }
  73.  
  74.         public string GoogleSeachGoBack()
  75.         {
  76.             try
  77.             {
  78.                 Click(GetElement("xpath", "//*[@id='pnprev']"));
  79.                 return "200";
  80.             }
  81.             catch (Exception)
  82.             {
  83.                 return "404";
  84.             }
  85.         }
  86.  
  87.         public void Exit()
  88.         {
  89.             driver.Quit();
  90.         }
  91.  
  92.         public void CheckBox(IWebElement element)
  93.         {
  94.             element.Click();
  95.         }
  96.  
  97.         public void Click(IWebElement element)
  98.         {
  99.             element.Click();
  100.         }
  101.  
  102.         public void Fill(IWebElement element, string text)
  103.         {
  104.             element.SendKeys(text);
  105.         }
  106.  
  107.         public void Wait(int delay)
  108.         {
  109.             Stopwatch sw = new Stopwatch();
  110.  
  111.             sw.Start();
  112.  
  113.             while (true)
  114.             {
  115.                 if (sw.Elapsed.Milliseconds >= delay) break;
  116.             }
  117.  
  118.             sw.Reset();
  119.         }
  120.  
  121.         public void Navigate(string url)
  122.         {
  123.             driver.Navigate().GoToUrl(url);
  124.         }
  125.  
  126.         public void ImplicitWait(int delay)
  127.         {
  128.             driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(delay);
  129.         }
  130.  
  131.         public void ToFrame(IWebElement element)
  132.         {
  133.             driver.SwitchTo().Frame(element);
  134.         }
  135.  
  136.         public void DefaultContent()
  137.         {
  138.             driver.SwitchTo().DefaultContent();
  139.         }
  140.  
  141.         public object InjectJs(string code)
  142.         {
  143.             return ((IJavaScriptExecutor)driver).ExecuteScript(code);
  144.         }
  145.  
  146.         public void Hide()
  147.         {
  148.             driver.Manage().Window.Position = new Point(-3000, 0);
  149.         }
  150.  
  151.         public void Show()
  152.         {
  153.             driver.Manage().Window.Position = new Point(0, 0);
  154.         }
  155.  
  156.         public void ScreenShot(string output)
  157.         {
  158.             Image img = GetEntireScreenshot();
  159.             img.Save(output);
  160.         }
  161.  
  162.         private Image GetEntireScreenshot()
  163.         {
  164.             // Get the total size of the page
  165.             var totalWidth = (int)(long)((IJavaScriptExecutor)driver).ExecuteScript("return document.body.offsetWidth"); //documentElement.scrollWidth");
  166.             var totalHeight = (int)(long)((IJavaScriptExecutor)driver).ExecuteScript("return  document.body.parentNode.scrollHeight");
  167.             // Get the size of the viewport
  168.             var viewportWidth = (int)(long)((IJavaScriptExecutor)driver).ExecuteScript("return document.body.clientWidth"); //documentElement.scrollWidth");
  169.             var viewportHeight = (int)(long)((IJavaScriptExecutor)driver).ExecuteScript("return window.innerHeight"); //documentElement.scrollWidth");
  170.  
  171.             // We only care about taking multiple images together if it doesn't already fit
  172.             if (totalWidth <= viewportWidth && totalHeight <= viewportHeight)
  173.             {
  174.                 var screenshot = driver.TakeScreenshot();
  175.                 return ScreenshotToImage(screenshot);
  176.             }
  177.             // Split the screen in multiple Rectangles
  178.             var rectangles = new List<Rectangle>();
  179.             // Loop until the totalHeight is reached
  180.             for (var y = 0; y < totalHeight; y += viewportHeight)
  181.             {
  182.                 var newHeight = viewportHeight;
  183.                 // Fix if the height of the element is too big
  184.                 if (y + viewportHeight > totalHeight)
  185.                 {
  186.                     newHeight = totalHeight - y;
  187.                 }
  188.                 // Loop until the totalWidth is reached
  189.                 for (var x = 0; x < totalWidth; x += viewportWidth)
  190.                 {
  191.                     var newWidth = viewportWidth;
  192.                     // Fix if the Width of the Element is too big
  193.                     if (x + viewportWidth > totalWidth)
  194.                     {
  195.                         newWidth = totalWidth - x;
  196.                     }
  197.                     // Create and add the Rectangle
  198.                     var currRect = new Rectangle(x, y, newWidth, newHeight);
  199.                     rectangles.Add(currRect);
  200.                 }
  201.             }
  202.             // Build the Image
  203.             var stitchedImage = new Bitmap(totalWidth, totalHeight);
  204.             // Get all Screenshots and stitch them together
  205.             var previous = Rectangle.Empty;
  206.             foreach (var rectangle in rectangles)
  207.             {
  208.                 // Calculate the scrolling (if needed)
  209.                 if (previous != Rectangle.Empty)
  210.                 {
  211.                     var xDiff = rectangle.Right - previous.Right;
  212.                     var yDiff = rectangle.Bottom - previous.Bottom;
  213.                     // Scroll
  214.                     ((IJavaScriptExecutor)driver).ExecuteScript(String.Format("window.scrollBy({0}, {1})", xDiff, yDiff));
  215.                 }
  216.                 // Take Screenshot
  217.                 var screenshot = driver.TakeScreenshot();
  218.                 // Build an Image out of the Screenshot
  219.                 var screenshotImage = ScreenshotToImage(screenshot);
  220.                 // Calculate the source Rectangle
  221.                 var sourceRectangle = new Rectangle(viewportWidth - rectangle.Width, viewportHeight - rectangle.Height, rectangle.Width, rectangle.Height);
  222.                 // Copy the Image
  223.                 using (var graphics = Graphics.FromImage(stitchedImage))
  224.                 {
  225.                     graphics.DrawImage(screenshotImage, rectangle, sourceRectangle, GraphicsUnit.Pixel);
  226.                 }
  227.                 // Set the Previous Rectangle
  228.                 previous = rectangle;
  229.             }
  230.             return stitchedImage;
  231.         }
  232.  
  233.         private Image ScreenshotToImage(Screenshot screenshot)
  234.         {
  235.             Image screenshotImage;
  236.             using (var memStream = new MemoryStream(screenshot.AsByteArray))
  237.             {
  238.                 screenshotImage = Image.FromStream(memStream);
  239.             }
  240.             return screenshotImage;
  241.         }
  242.  
  243.         public void FakePerson()
  244.         {
  245.             WebClient wc = new WebClient();
  246.  
  247.             ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
  248.  
  249.             string source = wc.DownloadString("http://api.namefake.com/");
  250.  
  251.             JObject j = JObject.Parse(source);
  252.  
  253.             name = j["name"].ToString();
  254.             anddress = j["address"].ToString();
  255.             username = j["username"].ToString();
  256.             password = j["password"].ToString();
  257.             phone = j["phone_h"].ToString();
  258.         }
  259.  
  260.         public List<string> ScrapEmail()
  261.         {
  262.             var pageSource = driver.PageSource;
  263.             var regex = new Regex(@"[\w\.-]+@[\w\.-]+");
  264.  
  265.             foreach (Match match in regex.Matches(pageSource))
  266.             {
  267.                 EmailList.Add(match.Value);
  268.             }
  269.  
  270.             return EmailList;
  271.         }
  272.  
  273.         public List<string> RetrieveAllLinks()
  274.         {
  275.             ReadOnlyCollection<IWebElement> links = driver.FindElements(By.TagName("a"));
  276.  
  277.             foreach (IWebElement el in links)
  278.             {
  279.                 LinksList.Add(el.GetAttribute("href"));
  280.  
  281.             }
  282.  
  283.             return LinksList;
  284.         }
  285.  
  286.         public void ScrollTo(int y)
  287.         {
  288.             InjectJs(string.Format("return window.page{0}Offset;", y));
  289.         }
  290.     }
  291. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement