通过硒多moveToElement循环/ webdriver的C# [英] Looping through multiple moveToElement in selenium/webdriver c#

查看:752
本文介绍了通过硒多moveToElement循环/ webdriver的C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过遍布网络元素的鼠标内部的这些嵌套链接的点击。在第一次迭代的工作原理,但在这之后停止。我试着加入 Thread.sleep代码(XXXX); 但这并不能工作。这里是我的方法:

I am trying to click through all of these nested links inside a mouse over web element. The first iteration works, but it stops after that. I've tried adding Thread.Sleep(xxxx); but that doesn't work either. Here's my method:

public static bool TestFindAssets()
    {
        bool result = false;
        Actions action = new Actions(driver);
        var findAssetsClick = driver.FindElement(By.XPath("/html/body/div/header/nav/ul/li[3]/a"));
        var home = driver.FindElement(By.LinkText("Home"));
        try
        {

            for (int i = 1; i < 13; i++)
            {
                action.MoveToElement(findAssetsClick).Perform(); //Find Assets Link
                action.MoveToElement(driver.FindElement(By.XPath("xpath"))).Perform(); //By Type Link
                action.MoveToElement(driver.FindElement(By.XPath("otherPath"+i))).Click().Build().Perform(); //list of links

            }

             result = true;
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error occurred: ", ex);
            result = false;
        }

        return result;
    }



此外,这适用于一次迭代。任何帮助表示赞赏。

Again, this works for one iteration. Any help is appreciated.

推荐答案

而不是硬编码的索引号,你应该找到 FindElements目标元素然后循环,但并单击来回。
二想,你需要使用适当的等待时间,以确保元素都正确地加载。
三,需要寻找动态的元素不能简单地通过收集迭代,然后点击来回。这将刷新 DOM 并抛出 StaleElement 引用异常。

Instead of hard coded index number you should find the target elements with FindElements then loop though and click back and forth. Second think, you need to use proper wait time to make sure the elements are loaded properly. Third, Need to find the element on the fly cannot simply iterate through the collection and click back and forth. It will refresh the DOM and throw StaleElement reference exception.

下面是在做你正在尝试做的。

Here is a sample test which is doing the same thing you are trying to do

public void ClickThroughLinks()
{
    Driver.Navigate().GoToUrl("http://www.cnn.com/");
    //Maximize the window so that the list can be gathered successfully.
    Driver.Manage().Window.Maximize();

    //find the list
    By xPath = By.XPath("//h2[.='The Latest']/../li//a");
    IReadOnlyCollection<IWebElement> linkCollection = Driver.FindElements(xPath);

    for (int i = 0; i < linkCollection.Count; i++)
    {
        //wait for the elements to be exist
        new WebDriverWait(Driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementExists(xPath));

        //Click on the elements by index
        Driver.FindElements(xPath)[i].Click();
        Driver.Navigate().Back();
        Driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(10));
    }
}

这篇关于通过硒多moveToElement循环/ webdriver的C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆