【问题标题】:C# selenium - element not visible: Element is not currently visible and may not be manipulated error messageC# selenium - 元素不可见:元素当前不可见并且可能无法操作错误消息
【发布时间】:2018-09-17 02:07:14
【问题描述】:

我无法从下拉列表中选择一个选项,通过单击输入文本框可以看到该选项。我在 c# 中使用 Selenium。功能如下;

  • 打开网页
  • 点击目标文本框
  • 这会导致显示目的地下拉列表
  • 从此列表中选择一个选项
  • 然后将选项写入目标文本框

此列表的 HTML 是;

 <div id='dvCountryList'>
    <select class="country-list" id="DestinationPicker" multiple="multiple" name="DestinationPicker">
    <option value="AU">Australia</option>
    <option value="ID">Indonesia</option>
    <option value="FJ">Fiji</option>
    <option value="US">United States of America (includes Hawaii)</option>
    <option value="CN">China</option>
    <option value="XA">Worldwide</option>
    <option value="TH">Thailand</option>
    </select>
    </div>

我的测试代码如下;

IWebElement destination1 = driver.FindElement(By.ClassName("select2-search__field"));
destination1.Click();

IWebElement destination2 = driver.FindElement(By.ClassName("country-list"));
SelectElement country = new SelectElement(destination2);
country.SelectByValue("AU");

运行此测试会产生以下错误;

OpenQA.Selenium.ElementNotVisibleException : 元素不可见:元素当前不可见,不能被操作

我尝试在找到国家/地区列表类之前使用等待,但这没有帮助。我对 Selenium 还是很陌生,因此我将不胜感激任何帮助/反馈。谢谢。

【问题讨论】:

  • 点击后似乎出现了选择列表?弹出后可以检查选项吗?
  • 是的,必须单击输入框控件才能显示列表。我不确定您所说的“检查选项”是什么意思?
  • 这基本上是因为有超过 1 个具有相同类或 id 的元素并且您选择了不可见元素。看看这个stackoverflow.com/questions/10641535/…

标签: c# selenium selenium-webdriver webdriver html-select


【解决方案1】:

要识别 web 元素 destination2,您使用了 Locator Strategy 作为 driver.FindElement(By.ClassName("country-list"));,这可能无法唯一地识别元素。要识别元素,您可以使用以下代码块:

IWebElement destination2 = driver.FindElement(By.XPath("//select[@class='country-list' and @id='DestinationPicker']"));
SelectElement country = new SelectElement(destination2);
country.SelectByValue("AU");

【讨论】:

  • 使用它会产生以下错误消息 - OpenQA.Selenium.NoSuchElementException : no such element: Unable to locate element: {"method":"xpath","selector":"//select[@ class='country-list' 和 @id='DestinationPicker']"}
  • @Diren 在执行 DebanjanB 的代码之前,您是否在文本字段内执行了点击?
  • 是的,点击是在我上面代码的前两行中执行的。这在 DebanjanB 的代码之前运行
  • @Diren,当你评论我时,请把@符号写上我的名字,以便我收到通知。试试这个代码IWebElement destination2 = driver.FindElement(By.XPath("//select[@id='DestinationPicker']"));
  • @Rajagopalan,对不起,我会在以后包括这个,谢谢。我已经尝试过您的建议,但我仍然收到原始错误消息 - OpenQA.Selenium.ElementNotVisibleException:元素不可见:元素当前不可见,可能无法操作
猜你喜欢
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-03
  • 2018-06-30
相关资源
最近更新 更多