【发布时间】: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