Selenuim driver.Navigate()。back()在导航回单词和转发时打破HTML DOM

时间:2016-06-03 16:20:41

标签: c# selenium navigation

下面的代码点击每个按钮,然后点击按钮生成的动态链接,将我带到下一页但是当测试导航回来时,由于“未在缓存中找到元素 - 未能执行类似步骤”页面已经改变,因为它被查找“。我需要测试链接并在此过程中前后移动。

点击和导航代码

public static int ClickNestedLink(int ID, IList<string>allNestedlinks,IWebDriver _driver,string Url)
    {            
        //Find the product brand and click on its brand and click on product link
        foreach (string BrandName in allNestedlinks)
        {

            IList<string> ProductBrandButtonList = new List<string>();                 

            //Find different brands
            foreach (IWebElement ProductBrandButton in _driver.FindElements(By.ClassName("productSet")))
            {
                IWebElement ProductParent = ProductBrandButton.FindElement(By.ClassName("product-parent"));
                String test = ProductParent.GetAttribute("data-product-id");
                ProductBrandButtonList.Add(test);
                //Find links for a brand  
                IList<string> LinksCollection = new List<string>();

                foreach (IWebElement ImageLink in ProductParent.FindElements(By.ClassName("brand_image")))
                {                        
                    String ImageLike2 = ImageLink.GetAttribute("src");
                    LinksCollection.Add(ImageLike2);
                    ImageLink.Click();
                    IWebElement productBrandClick = ProductParent.FindElement(By.ClassName("product-brand"));
                    String Test2 = productBrandClick.Text;
                    productBrandClick.Click();
                    _driver.Navigate().Back();
                    Task.Delay(20000).Wait();                                            }
            }                
        }
        return (ID);
    }

2 个答案:

答案 0 :(得分:2)

ClickBack操作正在加载包含新元素的新页面,使之前的引用过时。 要解决此问题,您需要在每个循环中找到元素:

// iterate the products
for (int i = 0; ; i++) {
    var products = _driver.FindElements(By.CssSelector(".productSet .product-parent"));
    if (i >= products.Count)
        break;

    // get the product for the iteration
    var product = products[i];

    // iterate the links
    for (int j = 0; ; j++) {
        var links = _driver.FindElements(By.CssSelector(".productSet .product-parent .brand_image"));
        var brands = _driver.FindElements(By.CssSelector(".productSet .product-parent .product-brand"));
        if (j >= links.Count)
            break;

        // get the link and brand for the iteration
        var link = links[j];
        var brand = brands[j];

        // click and navigate back
    }
}

答案 1 :(得分:0)

解决方案

public static int ClickNestedLink2(int ID,IList allNestedlinks,IWebDriver _driver,string Url)         {

        var products = _driver.FindElements(By.CssSelector(".productSet .product-parent"));
        var productCount = products.Count;

        for (int i = 0; i < productCount; i++)
        {
            var product = _driver.FindElements(By.CssSelector(".productSet .product-parent"))[i];
            var brandButtonsCount = product.FindElements(By.CssSelector(".productSet .product-parent .brand_image")).Count;

            for (int btnIdx = 0; btnIdx < brandButtonsCount; btnIdx++)
            {
                product = _driver.FindElements(By.CssSelector(".productSet .product-parent"))[i];
                var brandButton = product.FindElements(By.CssSelector(".productSet .product-parent .brand_image"))[btnIdx];

                brandButton.Click();

                var brandNameAnchor = product.FindElement(By.CssSelector(".productSet .product-parent .product-brand"));
                brandNameAnchor.Click();

                _driver.Navigate().Back();

                OpenQA.Selenium.Support.UI.WebDriverWait wait = new WebDriverWait(_driver, new TimeSpan(0, 0, 10));
                wait.Until(driver1 => ((IJavaScriptExecutor)_driver).ExecuteScript("return document.readyState").Equals("complete"));

            }
        }
        return (ID = 0);
    }        
}