static void Main(string[] args) { IWebDriver driver = new OperaDriver(@"D:\OneDrive\Thang\HOCTAP\PUBLIC PROJECTS\SeleniumCSharp"); driver.Navigate().GoToUrl("http://www.demoqa.com/automation-practice-form"); // Locate by ID attribute var firstName = driver.FindElement(By.Id("firstName")); firstName.SendKeys("Thang"); // Locate by Name attribute var genders = driver.FindElements(By.Name("gender")); foreach (var gender in genders) { if (gender.GetAttribute("value") == "Male") { ClickOnButton(driver, gender); } } //// Locate by Class name attribute //var form = driver.FindElement(By.ClassName("practice-form-wrapper")); ////linkText //driver.FindElement(By.LinkText("Home")); ////partialLinkText //driver.FindElement(By.PartialLinkText("Ho")); //var list = driver.FindElements(By.TagName("a")); //driver.FindElement(By.CssSelector("input[id= ‘userName’]")); //driver.FindElement(By.XPath("//input[@id='userName']")); Console.ReadKey(); driver.Quit(); }
/// <summary> /// Using the Web Driver Manager /// </summary> private static void UsingDriverManager(DriverOption driverOption) { IWebDriver webDriver = null; IDriverConfig driverConfig = null; switch (driverOption) { case DriverOption.Chrome: driverConfig = new ChromeConfig(); webDriver = new ChromeDriver(); break; case DriverOption.Edge: driverConfig = new EdgeConfig(); webDriver = new EdgeDriver(); break; case DriverOption.Firefox: driverConfig = new FirefoxConfig(); webDriver = new FirefoxDriver("./"); break; case DriverOption.IE: driverConfig = new InternetExplorerConfig(); webDriver = new InternetExplorerDriver(); break; case DriverOption.Opera: driverConfig = new OperaConfig(); webDriver = new OperaDriver(); break; } new DriverManager().SetUpDriver(driverConfig); webDriver.Navigate().GoToUrl("https://www.google.com"); System.Console.WriteLine($"Title : {webDriver.Title}"); webDriver.Quit(); }
public static void Main(string[] args) { string currentUser = string.Empty; try { new DriverManager().SetUpDriver(new OperaConfig(), "Latest", Architecture.Auto); string OperaProfilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Opera Software\\Opera Stable"); var jsonText = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "accounts.json")); var jsonAccounts = JsonConvert.DeserializeObject <List <Account> >(jsonText); OperaOptions opt = new OperaOptions(); opt.PageLoadStrategy = PageLoadStrategy.Eager; opt.AddArgument($"user-data-dir={OperaProfilePath}"); opt.AddArguments(new[] { "--incognito" }); opt.AddAdditionalCapability("useAutomationExtension", false); opt.AddExcludedArgument("enable-automation"); OperaDriverService driverService = OperaDriverService.CreateDefaultService(); driverService.HideCommandPromptWindow = true; for (int i = 0; i <= jsonAccounts.Count; i++) { currentUser = jsonAccounts[i].name; KillAllDrivers(); IWebDriver driver = new OperaDriver(driverService, opt); try { driver.Url = "https://giris.hepsiburada.com"; driver.WaitForPageLoad(); IWebElement elMail = driver.IsElementPresent(By.XPath("//input[@id='txtUserName']")); IWebElement elPass = driver.IsElementPresent(By.XPath("//input[@id='txtPassword']")); IWebElement elBtnLogin = driver.IsElementPresent(By.XPath("//button[contains(text(),'Giriş')]")); if (elMail == null && elPass == null && elBtnLogin == null) { throw new Exception("CANNOT_FIND_LOGINPAGE"); } elMail.Click(); elMail?.Clear(); elMail?.SendKeys(jsonAccounts[i].name); WaitSomeSecond(1); elPass?.Clear(); elPass?.SendKeys(jsonAccounts[i].pass); WaitSomeSecond(1); var touchActions = new Actions(driver); touchActions.MoveToElement(elBtnLogin).Perform(); touchActions.SendKeys(Keys.Enter).Perform(); WaitSomeSecond(1); driver.WaitForPageLoad(); WaitSomeSecond(6); if (!CheckIsLoggedorRegistered(driver, true)) { throw new Exception("CANNOT_LOGIN"); } driver.Navigate().GoToUrl("https://hesabim.hepsiburada.com/iletisim-tercihlerim"); driver.WaitForPageLoad(); WaitSomeSecond(5); var mailNotify = driver.RunJsCommand("return document.querySelector('div.x0LYwM_8u4ipmQOzUpbEM:nth-child(2) > div:nth-child(2) > div:nth-child(1) > label:nth-child(1) > input');"); if (mailNotify != null) { var checkState = (bool)driver.RunJsCommand("return arguments[0].checked", new[] { mailNotify }); if (checkState) { driver.RunJsCommand("arguments[0].click()", new[] { mailNotify }); Console.WriteLine($"{jsonAccounts[i].name} başarılı"); WaitSomeSecond(3); } else { Console.WriteLine($"{jsonAccounts[i].name} zaten kapatılmış"); } } else { throw new Exception("CANNOT_FOUND_MAIL_NOTIFY"); } } catch (Exception e) { Console.WriteLine($"Hesap Adı : {jsonAccounts[i].name}"); Console.WriteLine(e); Thread.Sleep(TimeSpan.FromSeconds(new Random().Next(5, 10))); } driver.Quit(); Thread.Sleep(TimeSpan.FromSeconds(new Random().Next(30, 70))); } } catch (Exception e) { Console.WriteLine(e); } }