C#-Selenium:“嵌套元素引用:...”,带有嵌套的for循环

时间:2019-04-09 22:43:16

标签: c# selenium-webdriver

我在此测试方法的不同点获得了过时的参考,其想法是在页面中测试过滤器下拉菜单,该菜单下拉列表会刷新每个选择,并显示过滤后的产品,我将每个下拉列表的不同选择合并在一起,这是代码:

 [TestMethod]
    public void TestShopByCategory()
    {
        driver.Navigate().GoToUrl(TestHelper.SiteUrl() + "/Forms/Site/ShopByCategories.aspx?departmentID=4&categoryID=4&search=");
        SelectElement selectPrice = new SelectElement(driver.FindElement(By.Id("MainContent_ShopByCategory_ddl_Price")));//       .XPath("//*[contains(@id, 'MainContent_ShopByCategory_ddl_Price')]")));
        SelectElement selectInstock = new SelectElement(driver.FindElement(By.Id("MainContent_ShopByCategory_ddl_Instock"))); //.XPath("//*[contains(@id, 'MainContent_ShopByCategory_ddl_Instock')]")));
        SelectElement selectRating = new SelectElement(driver.FindElement(By.Id("MainContent_ShopByCategory_ddl_Rating")));//.XPath("//*[contains(@id, 'MainContent_ShopByCategory_ddl_Rating')]")));            
        //var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
        for (int i = 0; i < selectPrice.Options.Count; i++)
        {
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
            selectPrice.Options[i].Click();                
            selectPrice = new SelectElement(driver.FindElement(By.XPath("//select[contains(@id, 'ddl_Price')]")));
            selectInstock = new SelectElement(driver.FindElement(By.XPath("//select[contains(@id, 'ddl_Instock')]")));
            selectRating = new SelectElement(driver.FindElement(By.XPath("//select[contains(@id, 'ddl_Rating')]")));
            for (int j = 0; j < selectInstock.Options.Count; j++)
            {
                driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
                selectInstock.Options[j].Click();                    
                selectPrice = new SelectElement(driver.FindElement(By.XPath("//select[contains(@id, 'ddl_Price')]")));
                selectInstock = new SelectElement(driver.FindElement(By.XPath("//select[contains(@id, 'ddl_Instock')]")));
                selectRating = new SelectElement(driver.FindElement(By.XPath("//select[contains(@id, 'ddl_Rating')]")));
                for (int k = 0; k < selectRating.Options.Count; k++)
                {
                    driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
                    selectRating.Options[k].Click();                        
                    selectPrice = new SelectElement(driver.FindElement(By.XPath("//select[contains(@id, 'ddl_Price')]")));
                    selectInstock = new SelectElement(driver.FindElement(By.XPath("//select[contains(@id, 'ddl_Instock')]")));
                    selectRating = new SelectElement(driver.FindElement(By.XPath("//select[contains(@id, 'ddl_Rating')]")));
                    Debug.WriteLine
                            ($"Price: {selectPrice.SelectedOption.Text}, " +
                            $"Instock: {selectInstock.SelectedOption.Text}, " +
                            $"Rating: {selectRating.SelectedOption.Text}");
                }
            }               
        }
    }

我也使用隐式wait,这里引用过时的元素有什么问题?

0 个答案:

没有答案