public static IWebDriver Authenticate() { string email = "admin@test"; IWebDriver webDriver = new RemoteWebDriver(new Uri("http://localhost:9515"), DesiredCapabilities.Chrome()); //webDriver.Manage().Window.Maximize(); webDriver.Navigate().GoToUrl("http://dev.icms/Account/Login"); //Enter email address on login page IWebElement emailLogin = webDriver.FindElement(By.Id("Email")); emailLogin.Clear(); emailLogin.SendKeys(email); IWebElement authenticateButton = webDriver.FindElement(By.Id("requestauth")); authenticateButton.Click(); //this should have sent me an email //Let's pretend we got the email and check the server for the authtoken and plug it into the URL string token = HttpUtility.UrlEncode(TestUtilities.AuthenticationUtil.GetAuthToken(email)); string goToUrl = string.Format("http://dev.icms/account/authorize/?authtoken={0}&email={1}&returnUrl=%2f", token, email); webDriver.Quit(); webDriver = new RemoteWebDriver(new Uri("http://localhost:9515"), DesiredCapabilities.Chrome()); webDriver.Navigate().GoToUrl(goToUrl); //Check DOM to see if we are logged in IWebElement elem = webDriver.FindElement(By.CssSelector("h1")); //webDriver.Quit(); if (elem.Text == "Welcome Admin Development") { return webDriver; } return null; }
protected void DeleteItem(RemoteWebDriver browser) { IWebElement link = browser.FindElement(By.Id("blAHrefLink0")); link.Click(); System.Threading.Thread.Sleep(_testStepInterval); link = browser.FindElement(By.Id("DesktopDeleteButton")); link.Click(); System.Threading.Thread.Sleep(_testStepInterval); }
protected void Search(RemoteWebDriver browser) { browser.FindElement(By.Id("SearchTerm")).SendKeys("drive"); System.Threading.Thread.Sleep(_testStepInterval); IWebElement link = browser.FindElement(By.Id("searchButtonSubmit")); link.Click(); System.Threading.Thread.Sleep(_testStepInterval); link = browser.FindElement(By.Id("CancelSearchResultDisplay")); link.Click(); System.Threading.Thread.Sleep(_testStepInterval); browser.FindElement(By.Id("SearchTerm")).SendKeys("drive"); System.Threading.Thread.Sleep(_testStepInterval); link = browser.FindElement(By.Id("searchButtonSubmit")); link.Click(); System.Threading.Thread.Sleep(_testStepInterval); link = browser.FindElement(By.Id("ProcessEdit0")); link.Click(); System.Threading.Thread.Sleep(_testStepInterval); link = browser.FindElement(By.Id("DesktopDeleteButton")); link.Click(); System.Threading.Thread.Sleep(_testStepInterval); browser.FindElement(By.Id("SearchTerm")).SendKeys("drive"); System.Threading.Thread.Sleep(_testStepInterval); link = browser.FindElement(By.Id("searchButtonSubmit")); link.Click(); System.Threading.Thread.Sleep(_testStepInterval); link = browser.FindElement(By.Id("ProcessEdit0")); link.Click(); System.Threading.Thread.Sleep(_testStepInterval); link = browser.FindElement(By.Id("DesktopEditButton")); link.Click(); System.Threading.Thread.Sleep(_testStepInterval); browser.FindElement(By.Id("BIItemName")).SendKeys(" edited item value"); System.Threading.Thread.Sleep(_testStepInterval); IWebElement rankingItemSelect = browser.FindElement(By.Name("rankingItemSelect")); SelectElement selectElement = new SelectElement(rankingItemSelect); selectElement.SelectByText("Warm"); System.Threading.Thread.Sleep(_testStepInterval); link = browser.FindElement(By.Id("EditBIButtonSubmit")); link.Click(); System.Threading.Thread.Sleep(_testStepInterval); }
//private static ISelenium selenium; public static void setup() { IWebDriver driver = new RemoteWebDriver(new Uri("http://127.0.0.1:4444/wd/hub"),DesiredCapabilities.HtmlUnit()); //driver = new FirefoxDriver(); Selenium.WebDriverBackedSelenium s = new Selenium.WebDriverBackedSelenium(driver, @"http://site4.way2sms.com/content/index.html"); s.Start(); driver.Navigate().GoToUrl("http://site4.way2sms.com/content/index.html"); WaitForLinkTextPresent(driver, "► click here to go to way2sms.com", 40); driver.FindElement(By.Id("username")).SendKeys("9916089888"); driver.FindElement(By.Id("password")).SendKeys("suprwolf"); driver.FindElement(By.Id("button")).Click(); if (WaitIdPresent(driver, "quickclose1",40000)) { driver.FindElement(By.Id("quickclose1")).Click(); } driver.FindElement(By.Id("quicksms")).Click(); driver.SwitchTo().Frame("frame"); s.WaitForPageToLoad("30000"); driver.FindElement(By.Id("MobNo")).SendKeys("9916089888"); WaitIdPresent(driver, "textArea", 400); driver.FindElement(By.Id("textArea")).SendKeys("test"); driver.FindElement(By.Id("Send")).Submit(); driver.FindElement(By.LinkText("Logout")).Clear(); }
/// <summary> /// Log a user into the website as an administrator /// </summary> /// <param name="driver"></param> protected void SignInAsAdmin(RemoteWebDriver driver) { //Notice navigation is slightly different than the Java version //This is because 'get' is a keyword in C# driver.Navigate().GoToUrl(BaseUrl + "/User/SignIn"); // Find the text input element by its name driver.FindElement(By.Name("Email")).SendKeys(_adminEmail); driver.FindElement(By.Name("Password")).SendKeys(_adminPass); driver.FindElement(By.CssSelector("button.btn-primary")).Click(); driver.WaitFor(u => u.Url.Contains("Manage")); }
public void FindPrice() { IWebDriver driver_ = new RemoteWebDriver(new Uri("http://192.168.1.4:4444/wd/hub"), DesiredCapabilities.Firefox()); driver_.Navigate().GoToUrl("http://www.momoshop.com.tw"); driver_.FindElement(By.CssSelector("#keyword")).SendKeys(this.SearchKeyWord_); driver_.FindElement(By.CssSelector(".inputbtn")).Click(); Dictionary<string, Dictionary<ProductInfo.ProductInfo, string>> info = new Dictionary<string, Dictionary<ProductInfo.ProductInfo, string>>(); try { if (driver_.FindElements(By.XPath("//*[@id=\"BodyBase\"]/form[1]/div/div[2]/div[5]/ul/li")).ToList().Count == 0) { ReadProductDetail(driver_.Url, ref info); } else { var PageCount = driver_.FindElements(By.XPath("//*[@id=\"BodyBase\"]/form[1]/div/div[2]/div[5]/ul/li")).ToList().Count; for (var CurrentPage = 1; CurrentPage <= PageCount;) { List<IWebElement> products = driver_.FindElements(By.XPath("//*[@id=\"chessboard\"]/li/a")).ToList(); foreach (var product in products) { ReadProductDetail(product.GetAttribute("href"), ref info); } //Last Page if (CurrentPage == PageCount) { break; } //Next Page ++CurrentPage; driver_.FindElement(By.XPath(string.Format("//*[@id=\"BodyBase\"]/form[1]/div/div[2]/div[5]/ul/li[{0}]/a", CurrentPage))).Click(); } } } catch (NoSuchElementException) { } this.ProductDetail_.Add("momo", info); driver_.Quit(); if (LocalDriver_ != null) { LocalDriver_.Quit(); } return; }
protected void EditItem(RemoteWebDriver browser) { IWebElement link = browser.FindElement(By.Id("btnEdit0")); link.Click(); System.Threading.Thread.Sleep(_testStepInterval); browser.FindElement(By.Id("BIItemName")).SendKeys("edited item value"); System.Threading.Thread.Sleep(_testStepInterval); IWebElement rankingItemSelect = browser.FindElement(By.Name("rankingItemSelect")); SelectElement selectElement = new SelectElement(rankingItemSelect); selectElement.SelectByText("Warm"); System.Threading.Thread.Sleep(_testStepInterval); link = browser.FindElement(By.Id("EditBIButtonSubmit")); link.Click(); }
public static string[] SaveAndRecordSubmittedTags( RemoteWebDriver driver ) { // Save driver.FindElement( By.Id( "save_tags" ) ).Click(); // Stash the values we entered var tagsAsSubmitted = driver.FindElements( By.CssSelector( "input.tag_name" ) ).Select( tagNameEl => tagNameEl.GetAttribute( "value" ).Trim() ).ToArray(); // Wait for the success report new WebDriverWait( driver, TimeSpan.FromSeconds( 10 ) ).Until( d => { var messagesElement = driver.FindElement( By.Id( "messages" ) ); return messagesElement.Displayed && -1 != messagesElement.Text.IndexOf( "saved successfully", StringComparison.InvariantCultureIgnoreCase ); } ); return tagsAsSubmitted; }
public void Selenium_Server_Test() { IWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), DesiredCapabilities.Firefox()); driver.Url = "http://www.google.com"; var searchBox = driver.FindElement(By.Id("lst-ib")); searchBox.SendKeys("Lotus flower"); searchBox.SendKeys(Keys.Enter); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10)); var imagetext = driver.FindElement(By.LinkText("Kuvahaku")); Assert.That(imagetext.Text, Is.EqualTo("Kuvahaku")); imagetext.Click(); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10)); //avoid error on no such element var imagelistId = driver.FindElement(By.Id("rg_s")); var fouthdivclass = imagelistId.FindElements(By.CssSelector("[class='rg_di rg_el ivg-i']"))[1].FindElement(By.TagName("a")); fouthdivclass.Click(); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10)); var ImageLink = driver.FindElement(By.LinkText("Näytä kuva")); ImageLink.Click(); }
public void FindPrice() { IWebDriver driver = new RemoteWebDriver(new Uri("http://192.168.1.4:4444/wd/hub"), DesiredCapabilities.Firefox()); driver.Navigate().GoToUrl("http://www.pcstore.com.tw/"); Thread.Sleep(1000); //#id_search_word driver.FindElement(By.CssSelector("#id_search_word")).SendKeys(this.SearchKeyWord_); driver.FindElement(By.XPath("//*[@id=\"top-search\"]/img")).Click(); Dictionary<string, Dictionary<ProductInfo.ProductInfo, string>> info = new Dictionary<string, Dictionary<ProductInfo.ProductInfo, string>>(); for (; ; ) { List<IWebElement> ProductList = driver.FindElements(By.XPath("//*[@id=\"keyad-pro-right3\"]/div[1]/a")).ToList(); foreach (var product in ProductList) { ReadProductDetail(product.GetAttribute("href"), ref info); } try { driver.FindElement(By.XPath("//*[@id=\"container\"]/div[10]/table[1]/tbody/tr/td[5]/a")).Click(); Thread.Sleep(1000); } catch (System.Exception) { break; } } this.ProductDetail_.Add("PcHome", info); if (LocalDriver_ != null) { LocalDriver_.Quit(); } driver.Quit(); return; }
public void GivenThatINavigateToABlogPostThatContainsAnImage() { var driver = new RemoteWebDriver(new Uri("http://localhost"), DesiredCapabilities.HtmlUnitWithJavaScript()); // Find the text input element by its name driver.Navigate(); // Enter something to search for IWebElement element = driver.FindElement(By.Id("")); // Now submit the form. WebDriver will find the form for us from the element //element.S }
protected void AddItemFromEditMenu(RemoteWebDriver browser) { IWebElement link = browser.FindElement(By.Id("blAHrefLink0")); link.Click(); System.Threading.Thread.Sleep(_testStepInterval); link = browser.FindElement(By.Id("DesktopAddButton")); link.Click(); System.Threading.Thread.Sleep(_testStepInterval); browser.FindElement(By.Id("BIItemName")).SendKeys("Bucket List Item 2"); System.Threading.Thread.Sleep(_testStepInterval); IWebElement rankingItemSelect = browser.FindElement(By.Name("rankingItemSelect")); SelectElement selectElement = new SelectElement(rankingItemSelect); selectElement.SelectByText("Cool"); System.Threading.Thread.Sleep(_testStepInterval); link = browser.FindElement(By.Id("AddBIButtonSubmit")); link.Click(); }
public static void CreateCustomerShouldSucceed( RemoteWebDriver driver, AuthenticatingNavigator navigator ) { using ( driver.FinallyQuitGuard() ) // TODO improve this using http://xunit.codeplex.com/workitem/9798 ( WAS: http://xunit.codeplex.com/discussions/362097 ) { navigator.NavigateWithAuthenticate( driver, "Sp.Web.Consume/Customer/Create" ); WebDriverWait wait = new WebDriverWait( driver, TimeSpan.FromSeconds( 5 ) ); wait.Until( d => d.FindElement( By.Id( "create-view" ) ) ); IWebElement nameElement = driver.FindElement( By.Id( "name" ) ); string anonymousCustomerName = "anonymousName" + Guid.NewGuid(); nameElement.SendKeys( anonymousCustomerName ); IWebElement externalIdElement = driver.FindElement( By.Id( "externalId" ) ); externalIdElement.SendKeys( "anonymousExternalId" ); IWebElement createButton = driver.FindElement( By.Id( "add-customer" ) ); createButton.Click(); wait.Until( d => d .FindElement( By.Id( "messages" ) ) .Text.Contains( "Customer created successfully" ) ); } }
static void Main(string[] args) { DesiredCapabilities capability = null; IWebDriver driver = null; for (int i = 1; i < 4; i++) { switch (i) { case 1: capability = DesiredCapabilities.Firefox(); capability.SetCapability("BrowserName", "firefox"); break; case 2: capability = DesiredCapabilities.Chrome(); capability.SetCapability("BrowserName", "chrome"); break; case 3: capability = DesiredCapabilities.InternetExplorer(); capability.SetCapability("BrowserName", "internet explorer"); break; default: return; } driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), capability); driver.Manage().Window.Maximize(); driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(10)); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); driver.Navigate().GoToUrl("http://www.baidu.com"); IWebElement searchBox = driver.FindElement(By.Id("kw")); IWebElement searchButton = driver.FindElement(By.Id("su")); searchBox.SendKeys("Shinetech"); searchButton.Click(); driver.Dispose(); } }
public void Should_be_able_to_read_text_from_label_when_content_is_more_than_just_text() { var pagePath = Path.Combine(Environment.CurrentDirectory, "markup.html"); driver = new FirefoxDriver(); driver.Navigate().GoToUrl(pagePath); var container = driver.FindElement(By.ClassName("some-class")); var labels = container.FindElements(By.TagName("label")); // This works: Assert.That(labels[0].GetAttribute("class"), Is.EqualTo("some-other-class")); // But not this: Assert.That(labels[0].Text, Is.EqualTo("Alla kommuner")); }
public void TitleIsCorrect() { for (int i = 0; i < 30; i++) { //IWebDriver webDriver = new ChromeDriver(); //IWebDriver webDriver = new PhantomJSDriver(); // TODO ^^ So instead of this we do this: IWebDriver webDriver = new RemoteWebDriver(new Uri("http://localhost:4444"), new DesiredCapabilities()); // ^^ this is how you'd call a Selenium Grid server, the desired capability is used to ask for a particular browser (or set of configuration) // you will need to first run phantomjs.exe --webdriver=4444 // you might run a local instance simply by calling java -jar selenium-grid.jar or something, then register browsers with it // you could also call source labs, browserstack or whatever // When using ChromeDriver you could try reuisng a webdriver session, // but then you could get failing tests interfering with other tests, you can do some work to mitigation this and use things like cookejars to isolate tests // but with PhantomJS giving you a new session each time, you don't have to worry about it. // we had it so that failures would output a screenshot (that would get attached to the test result), this is available through the webdriver api and is support by PhantomJS // we also had the webdriver switchable by config, so that we could quickly switch to a full browser to troubleshoot bugs webDriver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30)); webDriver.Navigate().GoToUrl("http://localhost:3988/"); var header = webDriver.FindElement(By.CssSelector("div.jumbotron > h1")); header.Text.ShouldBe("ASP.NET"); } //var liCount = _webDriver.FindElement(By.Id("test-content")).FindElements(By.TagName("li")).Count; //liCount.ShouldBe(3); // This sort of fiddly stuff, i.e. elements with timeouts, domReady etc... are not as bad as I remember. // because FindElements will wait using the _webdriver implicit wait settings (set when we construct the WebDriver object) // but what if there was already an <li> in the list, it fails! what if the dom exists, but then we replace it with ajax // thats when you might do something explicit like this: //var fiveSecondWait = new WebDriverWait(_webDriver, TimeSpan.FromSeconds(5)); //fiveSecondWait.Until(wd => //{ // // do some checks // return true; //}); // We used to queue some javascript so that we know that all synchrounous task had been parse and completed by the browser and we were ready to rock }
public static void OpenHtmlUnitDriver() { // initialize a WebDriver instance //IWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), DesiredCapabilities.HtmlUnit()); IWebDriver driver = new RemoteWebDriver(DesiredCapabilities.HtmlUnit()); // load google search page driver.Navigate().GoToUrl("https://www.google.ca"); // print title Console.WriteLine("Page title: " + driver.Title); // enter search word and submit IWebElement element = driver.FindElement(By.Name("q")); element.SendKeys("Cheese"); element.Submit(); // print title Console.WriteLine("Page title: " + driver.Title); // quit the driver driver.Quit(); }
static void Main(string[] args) { Local local = new Local(); List<KeyValuePair<string, string>> options = new List<KeyValuePair<string, string>>() { new KeyValuePair<string, string>("key", BROWSERSTACK_ACCESS_KEY), //new KeyValuePair<string, string>("localIdentifier", "identifier"), //new KeyValuePair<string, string>("f", "C:\\Users\\Admin\\Desktop\\"), new KeyValuePair<string, string>("onlyAutomate", "true"), new KeyValuePair<string, string>("verbose", "true"), new KeyValuePair<string, string>("forcelocal", "true"), new KeyValuePair<string, string>("binarypath", "C:\\Users\\Admin\\Desktop\\BrowserStackLocal.exe"), new KeyValuePair<string, string>("logfile", "C:\\Users\\Admin\\Desktop\\local.log"), }; local.start(options); // Run WebDriver Tests IWebDriver driver; DesiredCapabilities capability = DesiredCapabilities.Firefox(); capability.SetCapability("browserstack.user", BROWSERSTACK_USERNAME); capability.SetCapability("browserstack.key", BROWSERSTACK_ACCESS_KEY); //capability.SetCapability("browserstack.localIdentifier", "identifier"); capability.SetCapability("browserstack.local", true); capability.SetCapability("build", "build"); driver = new RemoteWebDriver( new Uri("http://hub.browserstack.com/wd/hub/"), capability ); driver.Navigate().GoToUrl("http://www.google.com"); Console.WriteLine(driver.Title); IWebElement query = driver.FindElement(By.Name("q")); query.SendKeys("Browserstack"); query.Submit(); Console.WriteLine(driver.Title); driver.Quit(); local.stop(); Console.WriteLine("Test Completed."); Console.ReadLine(); }
static void DeletesAndAddsShouldBeEvidentOnRefresh( RemoteWebDriver driver, AuthenticatingNavigator navigator ) { using ( driver.FinallyQuitGuard() ) // TODO improve this using http://xunit.codeplex.com/workitem/9798 ( WAS: http://xunit.codeplex.com/discussions/362097 ) { navigator.NavigateWithAuthenticate( driver, "tag" ); // Once the 'Add New Tag' button becomes enabled, we know that all tags have been loaded WebDriverWait shortWait = new WebDriverWait( driver, TimeSpan.FromSeconds( 2 ) ); shortWait.Until( d => d.FindElement( By.Id( "add_new_tag" ) ).Enabled ); //TODO TP 1650 - an enabled 'Add New Tag' button doesn't guarantee that the rendering is finished in Chrome; doing some unconditional wait... Thread.Sleep( 1000 ); // Delete all except the first (if there is one) foreach ( IWebElement item in driver.FindElements( By.CssSelector( "button.delete" ) ).Skip( 1 ) ) item.Click(); shortWait.Until( d => d.FindElements( By.CssSelector( "input.tag_name" ) ).Count <= 1 ); // Add a fresh one; give it a name driver.FindElement( By.Id( "add_new_tag" ) ).Click(); var newTagInputElement = driver.FindElements( By.CssSelector( "input.tag_name" ) ).Last(); newTagInputElement.Clear(); newTagInputElement.Click(); var newTagName = new Fixture().CreateAnonymous<string>(); newTagInputElement.SendKeys( newTagName ); string[] tagsAsSubmitted = SaveAndRecordSubmittedTags( driver ); // Need a retry in case the initial load loads data which does not include the (eventually consistent) changes new WebDriverWaitIgnoringNestedTimeouts( driver, TimeSpan.FromSeconds( 7 ) ).Until( _ => { driver.Navigate().Refresh(); // Verify reloading the page shows the same tags pretty quickly return shortWait.Until( d => d.FindElement( By.Id( "add_new_tag" ) ).Enabled && tagsAsSubmitted.SequenceEqual( d.FindElements( By.CssSelector( "input.tag_name" ) ).Select( tagNameEl => tagNameEl.GetAttribute( "value" ).Trim() ) ) ); } ); } }
public void BVT_BrowserStack() { IWebDriver driver; driver = new RemoteWebDriver( new Uri("http://hub.browserstack.com/wd/hub/"), base.BrowserCapabilities); driver.Navigate().GoToUrl("http://www.google.com/ncr"); Console.WriteLine(driver.Title); IWebElement query = driver.FindElement(By.Name("q")); query.SendKeys("Browserstack"); query.Submit(); Console.WriteLine(driver.Title); if (false == driver.Title.Contains("Google")) { Assert.Fail("Expected: '{0}' and Actual: '{1}'", "Google", driver.Title); } else { Console.WriteLine("Expected: '{0}' and Actual: '{1}'", "Google", driver.Title); } driver.Quit(); }
public void UnitTest() { //IWebDriver wd = new RemoteWebDriver(DesiredCapabilities.Firefox()); //wd.Navigate().GoToUrl("http://113.128.163.253/securepay/account/login.aspx"); DesiredCapabilities caps = DesiredCapabilities.InternetExplorer(); caps.SetCapability("browser", "IE"); caps.SetCapability("browser_version", "11.0"); caps.SetCapability("os", "Windows"); caps.SetCapability("os_version", "10"); caps.SetCapability("resolution", "1024x768"); IWebDriver wd = new RemoteWebDriver(new Uri("http://113.128.163.253/securepay/account/login.aspx"), caps); try { wd.FindElement(By.Id("MainContent_UserName")).Click(); wd.FindElement(By.Id("MainContent_UserName")).Clear(); wd.FindElement(By.Id("MainContent_UserName")).SendKeys("Sudhakar"); wd.FindElement(By.Id("MainContent_Password")).Click(); wd.FindElement(By.Id("MainContent_Password")).Clear(); wd.FindElement(By.Id("MainContent_Password")).SendKeys("sudhakar"); wd.FindElement(By.Id("MainContent_UserEntry")).Click(); wd.FindElement(By.Id("MainContent_UserEntry")).Clear(); wd.FindElement(By.Id("MainContent_UserEntry")).SendKeys("646F2"); wd.FindElement(By.Id("MainContent_LoginButton")).Click(); wd.FindElement(By.LinkText("Virtual Card")).Click(); wd.FindElement(By.Id("ContentPlaceHolder1_btnAddNew")).Click(); wd.FindElement(By.Id("ContentPlaceHolder1_txtCardNumber")).Click(); wd.FindElement(By.Id("ContentPlaceHolder1_txtCardNumber")).Clear(); wd.FindElement(By.Id("ContentPlaceHolder1_txtCardNumber")).SendKeys("4566882593"); wd.FindElement(By.XPath("//div[@id='ContentPlaceHolder1_divAddCard']/table/tbody/tr[1]/td[5]")).Click(); wd.FindElement(By.Id("ContentPlaceHolder1_txtNameonCard")).Click(); wd.FindElement(By.Id("ContentPlaceHolder1_txtNameonCard")).Clear(); wd.FindElement(By.Id("ContentPlaceHolder1_txtNameonCard")).SendKeys("Sudhakar"); wd.FindElement(By.Id("ContentPlaceHolder1_txtPriority")).Click(); wd.FindElement(By.Id("ContentPlaceHolder1_txtPriority")).Clear(); wd.FindElement(By.Id("ContentPlaceHolder1_txtPriority")).SendKeys("2"); wd.FindElement(By.Id("ContentPlaceHolder1_txtPercentage")).Click(); wd.FindElement(By.Id("ContentPlaceHolder1_txtPercentage")).Clear(); wd.FindElement(By.Id("ContentPlaceHolder1_txtPercentage")).SendKeys("20"); if (!wd.FindElement(By.Id("ContentPlaceHolder1_chkConfirm")).Selected) { wd.FindElement(By.Id("ContentPlaceHolder1_chkConfirm")).Click(); } wd.FindElement(By.Id("ContentPlaceHolder1_btnSubmit")).Click(); wd.FindElement(By.LinkText("Products")).Click(); } finally { wd.Quit(); } }
public void FindPrice() { IWebDriver driver = new RemoteWebDriver(new Uri("http://192.168.1.4:4444/wd/hub"), DesiredCapabilities.Firefox()); //超級商城 driver.Navigate().GoToUrl("https://tw.mall.yahoo.com/"); driver.FindElement(By.XPath("//*[@id=\"srp-ac-bar\"]")).SendKeys(this.SearchKeyWord_); driver.FindElement(By.XPath("//*[@id=\"UHSearchProperty\"]")).Click(); Dictionary<string, Dictionary<ProductInfo.ProductInfo, string>> info = new Dictionary<string, Dictionary<ProductInfo.ProductInfo, string>>(); for (; ; ) { List<IWebElement> products = driver.FindElements(By.XPath("//*[@id=\"srp_result_list\"]/div/div/div[1]/a")).ToList(); foreach (var product in products) { ReadProductDetailMail(product.GetAttribute("href"), ref info); } try { if (driver.FindElement(By.XPath("//*[@id=\"srp_sl_result\"]/div[3]/ul")).FindElements( By.XPath(".//li/a")).ToList().Last().Text == "下一頁") { driver.FindElement(By.XPath("//*[@id=\"srp_sl_result\"]/div[3]/ul")).FindElements( By.XPath(".//li/a")).ToList().Last().Click(); } else { break; } products.Clear(); Thread.Sleep(1000); } catch (NoSuchElementException e) { break; } } //購物中心 driver.Navigate().GoToUrl("https://tw.buy.yahoo.com/"); driver.FindElement(By.XPath("//*[@id=\"srp-ac-bar\"]")).SendKeys(this.SearchKeyWord_); driver.FindElement(By.XPath("//*[@id=\"UHSearchProperty\"]")).Click(); List<IWebElement> buyproducts = driver.FindElements(By.XPath("//*[@id=\"srp_result_list\"]/div/div/div[1]/a")).ToList(); for (; ; ) { List<IWebElement> products = driver.FindElements(By.XPath("//*[@id=\"srp_result_list\"]/div/div/div[1]/a")).ToList(); foreach (var product in buyproducts) { ReadProductDetailBuy(product.GetAttribute("href"), ref info); } try { if (driver.FindElement(By.XPath("//*[@id=\"srp_sl_result\"]/div[3]/ul")).FindElements( By.XPath(".//li/a")).ToList().Last().Text == "下一頁") { driver.FindElement(By.XPath("//*[@id=\"srp_sl_result\"]/div[3]/ul")).FindElements( By.XPath(".//li/a")).ToList().Last().Click(); } else { break; } Thread.Sleep(1000); } catch (NoSuchElementException e) { break; } } this.ProductDetail_.Add("Yahoo", info); driver.Quit(); if (LocalDriver_ != null) { LocalDriver_.Quit(); } return; }