【问题标题】:Element not visible when calling click method调用click方法时元素不可见
【发布时间】:2019-01-13 11:07:36
【问题描述】:

我正在尝试使用以下代码打开 </li> 元素:

DropDownElementByText("language-dd", "Englisch");

public void DropDownElementByText(string elementId, string text)
    {
        using (var dr = new ChromeDriver {Url = "https://www.startpage.com/deu/advanced-search.html"})
        {
            dr.Manage().Window.Maximize();

            var wait = new WebDriverWait(dr, TimeSpan.FromSeconds(10));
            IWebElement languageDropDown = wait.Until(d => d.FindElement(By.XPath("//div[@id='" + elementId + "']/ul")));

            //new Actions(dr).MoveToElement(languageDropDown).Perform();

            ReadOnlyCollection<IWebElement> languages = languageDropDown.FindElements(By.TagName("li"));
            foreach (IWebElement li in languages)
            {
                IWebElement a = dr.FindElement(By.XPath("//div[@id='" + elementId + "']/ul/li/a[text()='" + text + "']"));

                if (a != null)
                {
                    new Actions(dr).MoveToElement(li).Perform();
                    //li.Click();
                    li.SendKeys(Keys.Enter);
                    break;
                }
            }

        }
    }

li.SendKeys(Keys.Enter)li.Click() 都给我“测试方法 SeleniumTests.SeleniumTests.TestMethod1 抛出异常: OpenQA.Selenium.ElementNotVisibleException: 元素不可见"

【问题讨论】:

    标签: c# selenium-chromedriver selenium3


    【解决方案1】:

    我不确定你为什么要创建 Iwebelement a 以及为什么要使用 SendKeys,如果点击有效的话。

    这是更新代码(对我有用):

     var dropdown = Driver.FindElement(By.XPath("//*[@id='language-dd']"));
    var coll = Driver.FindElements(By.XPath("//*[@id='language-dd']/ul/li"));
    foreach (var item in coll)
    {
        dropdown.Click();
        item.WaitForElementToBeClickable(30);
        item.Click();
    }
    
    public static void WaitForElementToBeClickable(this IWebElement element, int timeout)
    {
        new WebDriverWait(Driver, TimeSpan.FromSeconds(timeout)).Until(ExpectedConditions.ElementToBeClickable(element));
    }
    

    【讨论】:

    • 单击 languageDropDown 也会引发 ElementNotVisibleException。它不是一个选择元素。您可以在提供的 URL 上自行检查,因为它是一个公共站点。
    • IWebElement a 应该选择一种语言。更新后的代码有效 - 谢谢!
    • @Shabbazz 如果可行,请accept it as answer
    猜你喜欢
    • 2016-03-26
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多