static void view() { int dem = 1; //int numberLoop = Int32.Parse(ConfigurationManager.AppSettings["numberLoop"]); int numberLoop = 7; try { while (true) { ToolViewYoutubePro toolViewYoutube = new ToolViewYoutubePro(); ToolLog.writeLog("tao moi trinh duyet"); for (int i = 1; i <= numberLoop; i++) { toolViewYoutube.gotoGoogle(); toolViewYoutube.gotoYoutube(); toolViewYoutube.openNewTab(); toolViewYoutube.closeNewTab(0); ToolLog.writeLog("----------------------------------------------------------xem lan so: " + dem); dem++; } toolViewYoutube.closeBrowser(); ToolLog.writeLog("dong trinh duyet"); } } catch (Exception ex) { ToolLog.showError(ex); } }
public void gotoYoutube() { try { Thread.Sleep(ToolConfig.timeWait); driver.FindElement(By.CssSelector("[href*='" + ToolConfig.urlYoutube + "']")).SendKeys(Keys.Enter); waitBrowser(ToolConfig.timeWait); ToolLog.writeLog("da vao youtube"); waitBrowser(ToolConfig.timeWait); driver.FindElement(By.Name(ToolConfig.elementSearchYoutube)).SendKeys(ToolConfig.keySearchYoutube); waitBrowser(ToolConfig.timeWait); driver.FindElement(By.Name(ToolConfig.elementSearchYoutube)).SendKeys(Keys.Enter); ToolLog.writeLog("tim kiem" + ToolConfig.keySearchYoutube); waitBrowser(ToolConfig.timeWait); WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5)); var element = wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("[href*='" + ToolConfig.idVideo + "']"))); Actions action = new Actions(driver); ToolLog.writeLog("hover chuot" + " vào video chứa url video"); action.MoveToElement(element).Perform(); waitBrowser(ToolConfig.timeWait); driver.FindElement(By.CssSelector("[href*='" + ToolConfig.idVideo + "']")).Click(); ToolLog.writeLog("click vao video"); ToolLog.writeLog("đang xem: " + ToolConfig.idVideo); Thread.Sleep(ToolConfig.timeVideo * 1000); ToolLog.writeLog("đã xem " + ToolConfig.timeVideo + "s"); } catch (Exception ex) { ToolLog.showError(ex); } }
public void closeBrowser() { try { waitBrowser(2); driver.Quit(); waitBrowser(2); } catch (Exception ex) { ToolLog.showError(ex); } }
public void openNewTab() { try { ((IJavaScriptExecutor)driver).ExecuteScript("window.open();"); ToolLog.writeLog("mở tab mới"); waitBrowser(2); } catch (Exception ex) { ToolLog.showError(ex); } }
public void closeNewTab(int index = 0) { try { driver.SwitchTo().Window(driver.WindowHandles[index]); driver.Close(); ToolLog.writeLog("close tab " + index); //((IJavaScriptExecutor)driver).ExecuteScript("window.close();"); driver.SwitchTo().Window(driver.WindowHandles[index]); ToolLog.writeLog("swich tab " + index); waitBrowser(3); } catch (Exception ex) { ToolLog.showError(ex); } }
public void gotoGoogle() { try { waitBrowser(3); driver.Navigate().GoToUrl(ToolConfig.urlGoogle); ToolLog.writeLog("tao user moi"); waitBrowser(3); // nhap vao khung tim kiem driver.FindElement(By.Name(ToolConfig.elementSearch)).SendKeys(ToolConfig.keySearchGoogle); Thread.Sleep(ToolConfig.timeWait); driver.FindElement(By.Name(ToolConfig.elementSearch)).SendKeys(Keys.Enter); ToolLog.writeLog("tao thong tin trinh duyet moi"); waitBrowser(ToolConfig.timeWait); } catch (Exception ex) { ToolLog.showError(ex); } }
public ToolViewYoutubePro() { // can them profile cho that try { FirefoxOptions options = new FirefoxOptions(); FirefoxProfile profile = new FirefoxProfile(); string userAgent = randomUserAgent(); profile.SetPreference("general.useragent.override", userAgent); if (ToolConfig.ui != true) { options.AddArguments("--headless"); } options.Profile = profile; driver = new FirefoxDriver(options); ToolLog.writeLog("User Agent:" + userAgent); } catch (Exception ex) { ToolLog.showError(ex); } }
public void waitBrowser(int second) { ToolLog.writeLog("dang cho " + second + "s"); driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(second); Thread.Sleep(second * 1000); }