public List <Song> GetSearch() { List <Song> search_results = new List <Song>(); IReadOnlyCollection <IWebElement> search_content; List <string> search_content_ids = new List <string>(); var service = PhantomJSDriverService.CreateDefaultService(); service.HideCommandPromptWindow = true; IWebDriver driver = new PhantomJSDriver(service); driver.Url = "http://www.playzer.fr"; IJavaScriptExecutor js = (IJavaScriptExecutor)driver; js.ExecuteScript("arguments[0].click();", driver.FindElement(By.XPath("//*[@id='panel_search']/img"))); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); Thread.Sleep(TimeSpan.FromSeconds(1)); IWebElement search = driver.FindElement(By.Id("search_engine")); search.SendKeys(searchBox.Text); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); Thread.Sleep(TimeSpan.FromSeconds(2)); js.ExecuteScript("arguments[0].click();", driver.FindElement(By.XPath("//*[@id='search_clips_tab']"))); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); // Thread.Sleep(TimeSpan.FromSeconds(2)); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); IWebElement result = driver.FindElement(By.Id("search_results")); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); // Thread.Sleep(TimeSpan.FromSeconds(2)); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); search_content = result.FindElements(By.XPath("//div[@class='content transition search_item_content']")); // Thread.Sleep(TimeSpan.FromSeconds(2)); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); int i = 1; foreach (IWebElement ids in search_content) { string song_ID = ids.GetAttribute("id"); string title = driver.FindElement(By.XPath("//*[@id='" + song_ID + "']/div[2]")).Text; string artist = driver.FindElement(By.XPath("//*[@id='" + song_ID + "']/div[3]")).Text; string url = driver.FindElement(By.XPath("//*[@id='" + song_ID + "']/div[1]/img[2]")).GetAttribute("src"); search_results.Add(new Song(title, artist, song_ID, url, i)); i++; } driver.Close(); driver.Quit(); driver.Dispose(); return(search_results); }
private void GetChapterComicPage() { var service = PhantomJSDriverService.CreateDefaultService(); service.DiskCache = true; service.IgnoreSslErrors = true; service.HideCommandPromptWindow = true; service.LoadImages = false; service.ProxyType = "none"; service.LocalToRemoteUrlAccess = true; PhantomJSDriver driver = new PhantomJSDriver(service, new PhantomJSOptions(), TimeSpan.FromSeconds(120)); driver.Navigate().GoToUrl(Url); var element = driver.FindElementById("page_select"); foreach (var pages in driver.FindElements(By.XPath("//*[@id=\"page_select\"]/option"))) { var page = new ComicPage(); page.PageImgUrl = "https:" + pages.GetAttribute("value"); page.PageNo = pages.Text.GetNumber(); _comicPageList.Add(page); } driver.Close(); driver.Dispose(); }
private System.Net.Cookie SetUpPhantomJS(string link) { System.Net.Cookie cookie = new System.Net.Cookie(); // PhantomJS seems to only work with .NET 4.5 or below. PhantomJSOptions options = new PhantomJSOptions(); options.AddAdditionalCapability("IsJavaScriptEnabled", true); PhantomJSDriver driver = new PhantomJSDriver(options); // https://stackoverflow.com/questions/46666084/how-to-deal-with-javascript-when-fetching-http-response-in-c-sharp-using-httpweb // https://riptutorial.com/phantomjs driver.Url = link; driver.Navigate(); var cookies = driver.Manage().Cookies.AllCookies; foreach (var c in cookies) { if (c.Name.Equals("TSPD_101")) { cookie.Name = c.Name; cookie.Domain = c.Domain; cookie.Value = c.Value; cookie.Path = c.Path; cookie.Expires = DateTime.Now.AddHours(6); } } driver.Close(); driver.Dispose(); return(cookie); }
public void scrapCinemaName(string googleURL) { IWebDriver driver = new PhantomJSDriver(); var url = googleURL; driver.Navigate().GoToUrl(url); for (int i = 0; i < 5; i++) { //add current page cinemas var cinemasName = scrapOnePageCinema(driver); //add all allCinemas.AddRange(cinemasName); //Go goto next on current page try { var nextUrl = driver.FindElements(By.PartialLinkText("Next")).Last().GetAttribute("href"); driver.Navigate().GoToUrl(nextUrl); } catch (InvalidOperationException e) { //Console.WriteLine(e.Source); } } //close driver driver.Dispose(); }
static int Main(string[] args) { if (args.Length == 0) { Console.WriteLine("You must provide the URL to unit test web page as the first command line argument"); return(1); } string url = args[0]; PhantomJSDriver driver = new PhantomJSDriver(); try { IElementLocator locator = new QUnitElementLocator(); JsTestRunner runner = new JsTestRunner(driver, locator); bool allTestsPassed = runner.ProcessAllTests(url); int result = allTestsPassed ? 0 : 1; return(result); } finally { driver.Quit(); driver.Dispose(); } }
public void CloseBrowser() { _logger.Trace("CloseBrowser"); _driver?.Quit(); #if UseSeleniumWithGeckodriver _driver?.Dispose();//this is needed for close geckodriver.exe during closing application #endif }
public void CloseDriver() { if (JSDriver != null) { JSDriver.Quit(); JSDriver.Dispose(); } }
public void start(string url, string match_id) { this.url = url; this.match_id = match_id; goToURL(); driver.Dispose(); driver.Quit(); }
/// <summary> /// REFERENCES: /// other approach: /// /// http://stackoverflow.com/questions/18921099/add-proxy-to-phantomjsdriver-selenium-c /// PhantomJSOptions phoptions = new PhantomJSOptions(); /// phoptions.AddAdditionalCapability(CapabilityType.Proxy, "http://localhost:9999"); /// /// driver = new PhantomJSDriver(phoptions); /// - (24/04/2019) https://stackoverflow.com/a/52446115/3796898 /// /// </summary> /// <param name="targetPgUrl"></param> /// <param name="proxy"></param> /// <returns></returns> public static string GrabPage(string targetPgUrl, Tuple <string, string> proxy = null) { //var options = new ChromeOptions(); //var userAgent = "user_agent_string"; //options.AddArgument("--user-agent=" + userAgent); //IWebDriver driver = new ChromeDriver(options); IWebDriver driver = null; Exception exception = null; var content = string.Empty; try { var service = PhantomJSDriverService.CreateDefaultService(".\\"); service.HideCommandPromptWindow = true; if (proxy != null) { service.Proxy = $"{proxy.Item1}:{proxy.Item2}"; } driver = new PhantomJSDriver(service) { Url = targetPgUrl }; content = GrabPage((PhantomJSDriver)driver, targetPgUrl, proxy); return(content); } catch (Exception ex) { exception = ex; } finally { if (driver != null) { Task.Delay(2000).ContinueWith(t => { Thread.Sleep(1000); driver.Quit(); driver.Dispose(); }); } if (exception != null) { throw exception; } } return(content); }
protected override object DownloadImpl() { object bytes = new byte[0]; var driverService = PhantomJSDriverService.CreateDefaultService(); driverService.HideCommandPromptWindow = true; driverService.LoadImages = false; driverService.IgnoreSslErrors = true; if (Proxy != null) { driverService.ProxyType = "http"; driverService.Proxy = Proxy.ToString(); } PhantomJSDriver phantom = null; try { phantom = new PhantomJSDriver(driverService); phantom.Navigate().GoToUrl(new Uri(Url)); if (!String.IsNullOrEmpty(_cssElement)) { var wait = new WebDriverWait(phantom, TimeSpan.FromSeconds(_cssTimeout)); wait.Message = "Couldn't find element in page"; wait.Until(drv => drv.FindElement(By.CssSelector(_cssElement))); } var postObject = RunPostDownload(phantom); if (postObject != null) { bytes = postObject; } else { string html = phantom.PageSource; bytes = Encoding.UTF8.GetBytes(html); } } finally { if (phantom != null) { phantom.Dispose(); } } return(bytes); }
public void BasicUiHeadless() { IWebDriver driver = new PhantomJSDriver(); try { RunUiTest(driver); } finally { driver.Dispose(); } }
/// <summary/> protected virtual void Dispose(bool fDisposing) { System.Diagnostics.Debug.WriteLineIf(!fDisposing, "****** Missing Dispose() call for " + GetType() + ". *******"); if (fDisposing && !IsDisposed) { // dispose managed and unmanaged objects if (_driver != null) { _driver.Dispose(); } _driver = null; } IsDisposed = true; }
static void Main() { Console.SetWindowSize(50, 15); string username = File.ReadAllText(GetDirectory("/config.txt")); Console.WriteLine("Username: "******"http://github.com/" + username); Console.WriteLine("Connected."); Block[,] data = new Block[numWeeks, 7]; try { var calendar = driver.FindElement(By.ClassName("js-calendar-graph")) .FindElements(By.TagName("g")); int range = calendar.Count - numWeeks; for (int i = range; i < calendar.Count; i++) { int week = i - range; Console.Write('\r' + "Grabbing data..." + (i - range) * 10 + "%"); var rectElements = calendar[i].FindElements(By.TagName("rect")); int day = 0; foreach (var rect in rectElements) { string fill = rect.GetAttribute("fill"); int contribCount = int.Parse(rect.GetAttribute("data-count")); string date = rect.GetAttribute("data-date"); data[week, day] = new Block(contribCount, fill, date); day++; } } Console.WriteLine('\r' + "Grabbing data...100%"); } catch (NoSuchElementException e) { Console.WriteLine(e); } driver.Quit(); // close the webdriver driver.Dispose(); FillWeek(data); // add placeholders to fill current week Console.WriteLine("Done."); WriteToFile(data, GetDirectory("/data.txt")); WriteToFile(GetWeek(data, numWeeks - 1), GetDirectory("/week.txt")); Environment.Exit(0); // Exit program }
public void DisposeDriver() { if (currentDriver != null) { try { currentDriver.Quit(); currentDriver.Dispose(); } catch { } } if (ScenarioContext.Current.ContainsKey(Constants.CurrentDriverKey)) { ScenarioContext.Current.Remove(Constants.CurrentDriverKey); } }
protected virtual void Dispose(bool disposing) { if (isDisposed) { return; } if (disposing) { LogHelper.WriteLine("Dispose - Phantom Durduruluyor!", ConsoleColor.White); if (driver != null) { driver.Dispose(); } LogHelper.WriteLine("Dispose - Phantom Durduruldu!", ConsoleColor.White); } isDisposed = true; }
static void Main(string[] args) { if (args.Length < 2) { Console.WriteLine("SoloLearn username password"); Environment.Exit(1); } string userName = args[0]; string passWord = args[1]; Dictionary <string, int> countries = GetCountries(); var driver = new PhantomJSDriver(); driver.Navigate().GoToUrl(loginUrl); var userID = driver.FindElementById("Email"); userID.Clear(); userID.SendKeys(userName); var passID = driver.FindElementById("Password"); passID.Clear(); passID.SendKeys(passWord); var form = driver.FindElementByTagName("form"); form.Submit(); driver.Navigate().GoToUrl(scrapeUrl); int delay = 3000; // 3 seconds var javascriptInBrowser = (IJavaScriptExecutor)driver; var detailsWrapper = driver.FindElementsByClassName("detailsWrapper"); var lastDetailsWrapperCount = detailsWrapper.Count; while (true) { javascriptInBrowser.ExecuteScript("arguments[0].scrollIntoView(true);", detailsWrapper[lastDetailsWrapperCount - 1]); Thread.Sleep(delay); detailsWrapper = driver.FindElementsByClassName("detailsWrapper"); if (detailsWrapper.Count == lastDetailsWrapperCount) { break; } else { lastDetailsWrapperCount = detailsWrapper.Count; delay += 500; // add 1/2 a second to the delay } } string text = string.Empty; foreach (var detail in detailsWrapper) { text = detail.GetAttribute("innerText").ToUpperInvariant(); foreach (KeyValuePair <string, int> kvp in countries) { if (text.Contains(kvp.Key.ToUpperInvariant())) { countries[kvp.Key] += 1; break; // don't look for multiple matches } } } driver.Quit(); driver.Dispose(); var items = from pair in countries orderby pair.Value descending select pair; foreach (KeyValuePair <string, int> kvp in items) { Console.WriteLine("{0}\t{1}", kvp.Key, kvp.Value); } }
public ActionResult DelInvalidCoupon(DateTime?date = null, int mode = 0) { System.Diagnostics.Stopwatch oTime = new System.Diagnostics.Stopwatch(); oTime.Start(); if (date == null) { date = DateTime.Now.Date.AddDays(-1); } var query = from c in db.Coupons let link = db.CouponUsers.FirstOrDefault(s => s.CouponID == c.ID).Link where (c.Platform == Enums.CouponPlatform.TaoBao || c.Platform == Enums.CouponPlatform.TMall) && c.CreateDateTime <date && c.EndDateTime> DateTime.Now select new { c.ID, Link = link }; var count = query.Count(); int pageSize = 50; int totalPage = count / pageSize + (count % pageSize > 0 ? 1 : 0); int hasCounpon = 0, noCounpon = 0, noLoadCount = 0, delCount = 0; var driver = new PhantomJSDriver(); List <int> invalidCouponIds = new List <int>(); try { for (int i = 1; i <= pageSize; i++) { var data = query.OrderBy(s => s.ID).ToPagedList(i, pageSize); foreach (var item in data) { if (string.IsNullOrWhiteSpace(item.Link)) { invalidCouponIds.Add(item.ID); continue; } driver.Url = $"{item.Link}"; driver.Navigate(); var wait = new OpenQA.Selenium.Support.UI.WebDriverWait(driver, TimeSpan.FromSeconds(1)); try { wait.Until(s => s.FindElement(OpenQA.Selenium.By.CssSelector($".atom-dialog,.coupons-wrap,.coupons-container-no"))); } catch (Exception) { noLoadCount++; } var source = driver.PageSource; var dom = CQ.CreateDocument(source); if (dom.Select(".coupons-price").Select(s => s.ClassName).ToList().Count > 0) { hasCounpon++; } else { invalidCouponIds.Add(item.ID); } //msg.Add(new SmsResult() //{ // IsSuccess = sdsd.Count > 0 ? true : false, // Message = item.ID.ToString(), //}); } } } finally { driver.Quit(); driver.Dispose(); } noCounpon = invalidCouponIds.Count(); if (mode > 0) { var size = 50; var tSize = invalidCouponIds.Count / size + (invalidCouponIds.Count % size > 0 ? 1 : 0); for (int i = 1; i <= tSize; i++) { var ids = invalidCouponIds.ToPagedList(i, size).ToList(); var coupons = db.Coupons .Where(s => ids.Contains(s.ID)) .ToList(); coupons.ForEach(s => s.EndDateTime = date.Value); db.SaveChanges(); } ; if (mode > 1) { var del = db.Coupons.Where(s => s.EndDateTime < DateTime.Now); db.Coupons.RemoveRange(del); delCount = db.SaveChanges(); } } oTime.Stop(); var result = new { //msg, HasCounpon = hasCounpon, NoCounpon = noCounpon, Total = count, NoLoad = noLoadCount, Delete = delCount, Time = oTime.Elapsed.TotalSeconds, }; Comm.WriteLog("DelInvalidCoupon", JsonConvert.SerializeObject(result), Enums.DebugLogLevel.Normal); return(Json(Comm.ToJsonResult("Success", "成功", result), JsonRequestBehavior.AllowGet)); }
//统计券是否有券 public ActionResult HasCoupon(DateTime?date) { System.Diagnostics.Stopwatch oTime = new System.Diagnostics.Stopwatch(); oTime.Start(); date = date.HasValue ? date : DateTime.Now; var query = from c in db.Coupons join uc in db.CouponUsers on c.ID equals uc.CouponID into ucc from ucg in ucc.DefaultIfEmpty() where (c.Platform == Enums.CouponPlatform.TaoBao || c.Platform == Enums.CouponPlatform.TMall) && c.CreateDateTime <= date.Value && c.EndDateTime <= date.Value select new { c.ID, Link = ucg.Link, Days = DbFunctions.DiffDays(c.CreateDateTime, date.Value) }; var count = query.Count(); int pageSize = 50; int totalPage = count / pageSize + (count % pageSize > 0 ? 1 : 0); var driver = new PhantomJSDriver(); List <VerCode> invalidCouponIds = new List <VerCode>(); try { Action <int> addInvalid = id => { invalidCouponIds.Add(new VerCode() { IsSuccess = false, Message = id.ToString() }); }; for (int i = 1; i <= totalPage; i++) { var data = query.OrderByDescending(s => s.Days).ThenBy(s => s.ID).ToPagedList(i, pageSize); foreach (var item in data) { if (string.IsNullOrWhiteSpace(item.Link)) { addInvalid(item.Days.Value); continue; } driver.Url = $"{item.Link}"; driver.Navigate(); var wait = new OpenQA.Selenium.Support.UI.WebDriverWait(driver, TimeSpan.FromSeconds(1)); try { wait.Until(s => s.FindElement(OpenQA.Selenium.By.CssSelector($".atom-dialog,.coupons-wrap,.coupons-container-no"))); } catch (Exception) { continue; } var source = driver.PageSource; var dom = CQ.CreateDocument(source); if (dom.Select(".coupons-price").Select(s => s.ClassName).ToList().Count > 0) { invalidCouponIds.Add(new VerCode() { IsSuccess = true, Message = item.Days.ToString() }); } else { addInvalid(item.Days.Value); } } } } finally { driver.Quit(); driver.Dispose(); } var model = invalidCouponIds.GroupBy(s => s.Message).Select(s => new { Days = s.Key, HasCoupon = s.Count(x => x.IsSuccess), NoCoupon = s.Count(x => !x.IsSuccess) }).OrderBy(s => s.Days); oTime.Stop(); Comm.WriteLog("CouponStatistics", new { data = JsonConvert.SerializeObject(model), Time = oTime.Elapsed.TotalSeconds }.ToString(), Enums.DebugLogLevel.Normal); return(Json(Comm.ToJsonResult("Success", "成功"), JsonRequestBehavior.AllowGet)); }
public List <MyPlaylist> GetMyPlaylist() { List <MyPlaylist> myplaylists = new List <MyPlaylist>(); MyPlaylist playlist1 = new MyPlaylist(); var service = PhantomJSDriverService.CreateDefaultService(); service.HideCommandPromptWindow = true; IWebDriver driver = new PhantomJSDriver(service); driver.Url = "http://www.playzer.fr/customer/login"; IJavaScriptExecutor js = (IJavaScriptExecutor)driver; driver.FindElement(By.Id("customer_login_login")).SendKeys(Email); driver.FindElement(By.Id("customer_login_password")).SendKeys(Pass); IWebElement btn = driver.FindElements(By.XPath(".//button[@onclick]"))[1]; js.ExecuteScript("arguments[0].click();", btn); // driver.Manage().Cookies.AddCookie(GetCookies()); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); IWebElement nav_menu = driver.FindElement(By.Id("nav_menu")); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); js.ExecuteScript("arguments[0].click();", nav_menu); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); js.ExecuteScript("arguments[0].click();", driver.FindElement(By.XPath("//*[@id='nav_menu']/ul"))); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); js.ExecuteScript("arguments[0].click();", driver.FindElement(By.XPath("//*[@id='nav_menu']/ul/div[1]"))); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); js.ExecuteScript("arguments[0].click();", driver.FindElement(By.XPath("//*[@id='nav_menu']/ul/div[1]/div[2]"))); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); js.ExecuteScript("arguments[0].click();", driver.FindElement(By.Id("menu_myplaylists"))); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10)); IReadOnlyCollection <IWebElement> all_playlist = driver.FindElements(By.XPath("//div[@class='content home_playlist transition']")); Thread.Sleep(TimeSpan.FromSeconds(2)); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10)); int i = 1; foreach (IWebElement playlist in all_playlist) { string id = playlist.GetAttribute("id"); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(2)); IWebElement playlist_option_btn = driver.FindElement(By.XPath("//*[@id='" + id + "_options" + "']")); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(2)); js.ExecuteScript("arguments[0].click();", playlist_option_btn); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(2)); IWebElement edit_btn = driver.FindElement(By.XPath("//div[@class='edit transition']")); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(2)); js.ExecuteScript("arguments[0].click();", edit_btn); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(2)); IWebElement song_items = driver.FindElement(By.XPath("//ul[@id='playlist_items']")); //Thread.Sleep(TimeSpan.FromSeconds(2)); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(2)); IReadOnlyCollection <IWebElement> songs = song_items.FindElements(By.ClassName("content")); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(2)); playlist1 = new MyPlaylist(); playlist1.Title = driver.FindElement(By.XPath("//*[@id='playlist_edit_name']")).GetAttribute("value"); playlist1.SelectedId = i; foreach (IWebElement song in songs) { driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(2)); string id_song = song.GetAttribute("id"); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); string title = driver.FindElement(By.XPath("//*[@id='" + id_song + "']/div[4]")).Text; driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); string artist = driver.FindElement(By.XPath("//*[@id='" + id_song + "']/div[5]")).Text; driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); string url = driver.FindElement(By.XPath("//*[@id='" + id_song + "']/div[3]/img")).GetAttribute("src"); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); Song track = new Song(title, artist, id_song, url, i); playlist1.Songs.Add(track); playlist1.NumOfSongs = songs.Count; playlist1.Url = url; } driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); myplaylists.Add(playlist1); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); js.ExecuteScript("arguments[0].click();", driver.FindElement(By.Id("close_modify"))); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); js.ExecuteScript("arguments[0].click();", driver.FindElement(By.XPath("//*[@id='playlist_options']/div[1]/img"))); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); i++; } driver.Close(); driver.Dispose(); driver.Quit(); PlaylistDetailsWindow.all_playlists = myplaylists; myPlaylist = myplaylists; return(myplaylists); }
/// <summary> /// 获取网址的Html文档 /// </summary> /// <param name="targetUrl">目标网址</param> /// <returns></returns> private string GetHtmlDocument(string targetUrl) { string retHtmlDoc = string.Empty; if (string.IsNullOrEmpty(targetUrl)) { return(retHtmlDoc); } this.WriteMessage($"开始获取==>【{targetUrl}】网页文档的数据"); try { //创建代理 // WebProxy proxy = new WebProxy("113.121.245.231", 6675); // "107.150.96.188", 8080 //创建一个请求 HttpWebRequest httprequst = (HttpWebRequest)WebRequest.Create(targetUrl); //代理ip // httprequst.Proxy = proxy; //不建立持久性链接 httprequst.KeepAlive = true; //设置请求的方法 httprequst.Method = "GET"; //设置标头值 httprequst.UserAgent = "User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705"; httprequst.Accept = "*/*"; httprequst.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.5"); httprequst.ServicePoint.Expect100Continue = false; httprequst.Timeout = 5000; httprequst.AllowAutoRedirect = true;//是否允许302 ServicePointManager.DefaultConnectionLimit = 30; //获取响应 HttpWebResponse webRes = (HttpWebResponse)httprequst.GetResponse(); //获取响应的文本流 using (System.IO.Stream stream = webRes.GetResponseStream()) { using (System.IO.StreamReader reader = new StreamReader(stream, System.Text.Encoding.GetEncoding("utf-8"))) { retHtmlDoc = reader.ReadToEnd(); } } //取消请求 httprequst.Abort(); //返回数据内容 return(retHtmlDoc); } catch (Exception ex) { this.WriteMessage($"获取网页文档时出错,还在尝试……"); try { PhantomJSDriverService services = PhantomJSDriverService.CreateDefaultService(); services.HideCommandPromptWindow = true;//隐藏控制台窗口 IWebDriver driver = new PhantomJSDriver(services); driver.Navigate().GoToUrl(targetUrl); retHtmlDoc = driver.PageSource; //Console.WriteLine(driver.PageSource); //Console.Read(); driver.Quit(); driver.Dispose(); } catch (Exception e) { this.WriteMessage($"最终尝试失败!!!!"); } return(retHtmlDoc); } }
public bool Login(string email1, string password) { List <MyPlaylist> myplaylists = new List <MyPlaylist>(); MyPlaylist playlist1 = new MyPlaylist(); List <MyPlaylist> myPlaylist = new List <MyPlaylist>(); var service = PhantomJSDriverService.CreateDefaultService(); service.HideCommandPromptWindow = true; IWebDriver driver = new PhantomJSDriver(service); driver.Url = "http://www.playzer.fr/customer/login"; driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); Thread.Sleep(TimeSpan.FromSeconds(1)); driver.FindElement(By.Id("customer_login_login")).SendKeys(email1); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); driver.FindElement(By.Id("customer_login_password")).SendKeys(password); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); IWebElement btn = driver.FindElements(By.XPath(".//button[@onclick]"))[1]; IJavaScriptExecutor js = (IJavaScriptExecutor)driver; js.ExecuteScript("arguments[0].click();", btn); Thread.Sleep(TimeSpan.FromSeconds(1)); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); driver.Navigate().GoToUrl("http://www.playzer.fr/customer/account"); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); string s = driver.FindElement(By.XPath("//*[@id='account']/div/h2[1]")).Text; Thread.Sleep(TimeSpan.FromSeconds(1)); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); if ("You are not logged in" == s) { return(false); } else { if (!File.Exists(email1 + "Playlists.txt")) { driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); IWebElement nav_menu = driver.FindElement(By.Id("nav_menu")); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); js.ExecuteScript("arguments[0].click();", nav_menu); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); js.ExecuteScript("arguments[0].click();", driver.FindElement(By.XPath("//*[@id='nav_menu']/ul"))); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); js.ExecuteScript("arguments[0].click();", driver.FindElement(By.XPath("//*[@id='nav_menu']/ul/div[1]"))); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); js.ExecuteScript("arguments[0].click();", driver.FindElement(By.XPath("//*[@id='nav_menu']/ul/div[1]/div[2]"))); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); js.ExecuteScript("arguments[0].click();", driver.FindElement(By.Id("menu_myplaylists"))); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10)); IReadOnlyCollection <IWebElement> all_playlist = driver.FindElements(By.XPath("//div[@class='content home_playlist transition']")); Thread.Sleep(TimeSpan.FromSeconds(1)); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10)); int i = 1; foreach (IWebElement playlist in all_playlist) { string id = playlist.GetAttribute("id"); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(2)); IWebElement playlist_option_btn = driver.FindElement(By.XPath("//*[@id='" + id + "_options" + "']")); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(2)); js.ExecuteScript("arguments[0].click();", playlist_option_btn); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(2)); IWebElement edit_btn = driver.FindElement(By.XPath("//div[@class='edit transition']")); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(2)); js.ExecuteScript("arguments[0].click();", edit_btn); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(2)); IWebElement song_items = driver.FindElement(By.XPath("//ul[@id='playlist_items']")); Thread.Sleep(TimeSpan.FromSeconds(1)); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(2)); IReadOnlyCollection <IWebElement> songs = song_items.FindElements(By.ClassName("content")); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(2)); playlist1 = new MyPlaylist(); playlist1.Title = driver.FindElement(By.XPath("//*[@id='playlist_edit_name']")).GetAttribute("value"); playlist1.SelectedId = i; foreach (IWebElement song in songs) { driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(2)); string id_song = song.GetAttribute("id"); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); string title = driver.FindElement(By.XPath("//*[@id='" + id_song + "']/div[4]")).Text; driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); string artist = driver.FindElement(By.XPath("//*[@id='" + id_song + "']/div[5]")).Text; driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); string url = driver.FindElement(By.XPath("//*[@id='" + id_song + "']/div[3]/img")).GetAttribute("src"); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); Song track = new Song(title, artist, id_song, url, i); playlist1.Songs.Add(track); playlist1.NumOfSongs = songs.Count; playlist1.Url = url; } driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); myplaylists.Add(playlist1); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); js.ExecuteScript("arguments[0].click();", driver.FindElement(By.Id("close_modify"))); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); js.ExecuteScript("arguments[0].click();", driver.FindElement(By.XPath("//*[@id='playlist_options']/div[1]/img"))); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); i++; } driver.Close(); driver.Dispose(); driver.Quit(); PlaylistDetailsWindow.all_playlists = myplaylists; MainWindow.allPL = myplaylists; using (StreamWriter theWriter = new StreamWriter(email1 + "Playlists.txt")) { try { int cntlist = 0; foreach (MyPlaylist mp in myplaylists) { cntlist++; theWriter.WriteLine("PLAYLIST " + cntlist + "*#" + mp.Title + "*#" + mp.Url); theWriter.WriteLine(); int cntSong = 0; foreach (Song song in mp.Songs) { cntSong++; theWriter.WriteLine("SONG " + cntSong + "|?" + song.Id + "|?" + song.Title + "|?" + song.Artist + "|?" + song.Url); } } } catch (Exception ex) { } } using (StreamWriter theWriter = new StreamWriter(email1 + ".txt")) { try { theWriter.Write(password); } catch (Exception ex) { } } } else { List <MyPlaylist> plejliste = new List <MyPlaylist>(); string path1 = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string text = File.ReadAllText(Path.Combine(path1, email1 + "Playlists.txt")); string[] plist = text.Split(new string[] { "PLAYLIST " }, StringSplitOptions.None); foreach (string lists in plist) { if (lists != "") { MyPlaylist mpl = new MyPlaylist(); int selected; string[] items = lists.Split(new string[] { "*#" }, StringSplitOptions.None); Int32.TryParse(items[0], out selected); mpl.SelectedId = selected; mpl.Title = items[1]; mpl.Url = items[2].Split('\n')[0].Split('\r')[0]; string[] tracks = items[2].Split(new string[] { "\r\n\r\n" }, StringSplitOptions.None); string[] tr = tracks[1].Split(new string[] { "\r\n" }, StringSplitOptions.None); foreach (string track in tr) { if (track != "") { string[] song_stuff = track.Split(new string[] { "|?" }, StringSplitOptions.None); int selectedtr; Int32.TryParse(song_stuff[0].Split(' ')[1], out selectedtr); Song song = new Song(song_stuff[2], song_stuff[3], song_stuff[1], song_stuff[4], selectedtr); mpl.NumOfSongs = tr.Count() - 1; mpl.Songs.Add(song); } } plejliste.Add(mpl); } } MainWindow.allPL = plejliste; } driver.Quit(); return(true); } }
public void TearDown() { driver.Quit(); driver.Dispose(); }