如何使用selenium(C#)自动化网格中的选择控件(点击和保持)

时间:2016-08-03 02:15:18

标签: c# selenium selenium-webdriver

我的代码是:

[TestMethod]
    public void ActionClickAndHold()
    {
        driver = new ChromeDriver();
        driver.Manage().Window.Maximize();
        driver.Navigate().GoToUrl("http://jqueryui.com/selectable/");

        driver.FindElement(By.Id("selectable")).Click();
        System.Threading.Thread.Sleep(10000);
        driver.SwitchTo().Frame(0);
        ReadOnlyCollection<IWebElement> lstItems = driver.FindElements(By.XPath("//*[@id='selectable']/li[1]"));
        Actions builder = new Actions(driver);
        builder.ClickAndHold(lstItems[1])
                .ClickAndHold(lstItems[3])
                .Click()
                .Perform();
        driver.SwitchTo().DefaultContent();
    }

它不起作用。它不会单击并保持在网格中。我在哪里知道自己的错误?

1 个答案:

答案 0 :(得分:1)

实际上你会遇到错误的元素,请尝试如下: -

driver = new ChromeDriver();
driver.Manage().Window.Maximize();
driver.Navigate().GoToUrl("http://jqueryui.com/selectable/");

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(3))

wait.Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.ClassName("demo-frame")));

ReadOnlyCollection<IWebElement> lstItems = wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.CssSelector("#selectable li")));

builder.ClickAndHold(lstItems[1])
                .ClickAndHold(lstItems[3])
                .Click()
                .Perform();
driver.SwitchTo().DefaultContent();

希望它有所帮助.. :)