【问题标题】:Element in dropdown not clickable exception after search (C# Selenium)搜索后下拉列表中的元素不可点击异常(C# Selenium)
【发布时间】:2021-06-02 00:24:00
【问题描述】:

我正在尝试为我的项目构建一个自动化框架,但我遇到了下面提到的问题。可以给点建议吗?

我有一个“某种”下拉菜单,其中包括国家列表的搜索输入。我搜索特定文本“美国”。显示两个结果。我想点击第二个。

初始下拉菜单:

搜索后的下拉菜单:

搜索结果的 HTML 代码:

<span class="select2-dropdown select2-dropdown--below" dir="ltr" style="width: 253.087px;">
<span class="select2-search select2-search--dropdown">
<input class="select2-search__field" type="search" tabindex="0" autocomplete="off" autocorrect="off" autocapitalize="none" spellcheck="false" role="searchbox" aria-autocomplete="list" placeholder="" aria-controls="select2-txtCountry-results" aria-activedescendant="select2-txtCountry-result-swi0-UM"></span>
<span class="select2-results">
<ul class="select2-results__options" role="listbox" id="select2-txtCountry-results" aria-expanded="true" aria-hidden="false">
<li class="select2-results__option select2-results__option--highlighted" id="select2-txtCountry-result-swi0-UM" role="option" aria-selected="false" data-select2-id="select2-txtCountry-result-swi0-UM">United States Minor Is.</li>
<li class="select2-results__option" id="select2-txtCountry-result-xg7o-US" role="option" aria-selected="false" data-select2-id="select2-txtCountry-result-xg7o-US">United States</li>
</ul>
</span>
</span>

这就是我做出选择的方式:

       protected void WaitForElement(IWebElement element, int timeout = 2)
       {
            WebDriverWait wait = new WebDriverWait(iWebDriver, TimeSpan.FromMinutes(timeout));
            wait.IgnoreExceptionTypes(typeof(NoSuchElementException));
            wait.IgnoreExceptionTypes(typeof(StaleElementReferenceException));
            wait.Until<bool>(driver =>
            {
                try
                {
                     return (element.Displayed && element.Enabled);
                }
                catch (Exception)
                {
                    return false;
                }
            });
        }

        protected IList<IWebElement> FindAll(By locator, int timeoutInSeconds = 5)
        {
            if (timeoutInSeconds > 0)
            {
                var wait = new WebDriverWait(iWebDriver, TimeSpan.FromSeconds(timeoutInSeconds));
                return wait.Until(drv => (drv.FindElements(locator).Count > 0) ? drv.FindElements(locator) : null);
            }
            return iWebDriver.FindElements(locator);
        }
        
        protected int getNumberOfElementsFound(By by)
        {
            return FindAll(by).Count();
        }

        protected IWebElement getElementWithIndex(By by, int pos)
        {
            return FindAll(by).ElementAt(pos);

        }

        protected void DropDownValueSelection(By containerLocator, By searchInputLocator, By dropdownItemsLocator, String value)
        {
                Click(containerLocator);
                Type(searchInputLocator, value);
                int numberOfElementsFound = getNumberOfElementsFound(dropdownItemsLocator);
                for (int pos = 0; pos < numberOfElementsFound ; pos++)
                {
                    IWebElement element = getElementWithIndex(dropdownItemsLocator, pos);
                    Console.WriteLine("element in for {0}", element.Text.ToString());
                    if (element.Text.Equals(value))
                    {
                        WaitForElement(element);
                        element.Click();
                        break;
                    }
                }
}

这是我使用的定位器:

By dropdownItems => By.XPath("//span//ul[@class='select2-results__options']/li");
By CountryInput => By.Id("select2-txtCountry-container");
By CountrySearchInput => By.ClassName("select2-search__field");

我认为问题可能出在“//ul//li”下拉项上方的跨度上。 我尝试在搜索输入中不输入文本的情况下搜索值,并且它可以工作,但它需要很长时间,因为国家列表中有超过 200 个条目。 我收到以下错误:

OpenQA.Selenium.ElementClickInterceptedException : 元素点击 拦截:元素在点 (445, 715) 处不可点击

我还检查了元素是否显示并启用,页面没有重新加载,除了跨度之外没有其他元素(加载器等)。 感谢您的意见。

【问题讨论】:

  • 请勿发布代码图片。我编辑了您的问题以包含屏幕截图,这是有用的信息。但是请edit您的问题是将HTML 包含为代码,而不是图像。
  • 谢谢,我已经编辑了帖子并添加了HTML代码。
  • 能否添加WaitForElement方法的源代码?
  • 我已经在代码开头添加了方法。谢谢。

标签: c# selenium click dropdown


【解决方案1】:

有时您只需要单击某些内容即可成功。我不知道为什么,但是 Selenium 没有带有 WebDriverWait.Until(...) 的重载,它具有 void 返回类型。我最终为 WebDriverWait 类创建了一个扩展方法以允许这样做:

public static class WebDriverWaitExtensions
{
    /// <summary>
    /// Waits until no exception is thrown.
    /// </summary>
    /// <param name="wait"></param>
    /// <param name="callback">The callback executed until no exception is thrown.</param>
    public static void Until(this WebDriverWait wait, Action<IWebDriver> callback)
    {
        wait.Until(driver =>
        {
            try
            {
                callback(driver);

                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        });
    }
}

现在只需等待点击成功:

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

// Using == easier to read than element.Text.Equals(value)
if (element.Text == value)
{
    wait.Until(d => element.Click());

    break;
}

【讨论】:

  • 非常感谢@Greg 我尝试实现扩展,但出现以下错误:严重性代码描述项目文件行抑制状态错误 CS0411 方法'DefaultWait.Until(Func)' 不能从用法中推断出来。尝试明确指定类型参数。
  • 抱歉,我无法格式化我的评论。我使用了stackoverflow教程,但标签似乎不起作用。
  • @MirelaM:确保此扩展方法类的命名空间是项目的根命名空间,或者在 C# 文件的顶部添加 using X; 指令,在其中替换“X " 使用这个类的命名空间。
猜你喜欢
  • 1970-01-01
  • 2018-02-19
  • 2019-01-05
  • 1970-01-01
  • 1970-01-01
  • 2020-08-26
  • 2016-07-08
  • 2016-11-27
  • 2021-04-02
相关资源
最近更新 更多