FindElements() 공개 메소드

Finds the elements on the page by using the By object and returns a ReadOnlyCollection of the Elements on the page
public FindElements ( By by ) : ReadOnlyCollection
by By By mechanism to find the element
리턴 ReadOnlyCollection
예제 #1
0
        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;
        }
예제 #2
0
		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() ) ) );
				} );
			}
		}
예제 #3
0
		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;
		}
예제 #4
0
        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;
        }
예제 #5
0
        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;
        }