【问题标题】:Not able to select option from drop down list in selenium webdriver with java无法从带有 java 的 selenium webdriver 的下拉列表中选择选项
【发布时间】:2018-08-24 01:24:31
【问题描述】:

我正在尝试从下拉列表中选择一个值。我已经检查了所有相关的帖子,但无法找到解决方案。

这是我的下拉列表的 HTML 代码:

<select class="select2 visible" data-val="true" data-val-number="The field ClientId must be a number." id="ClientId" name="ClientId" tabindex="-1" title="" style="display: none;">
<option value="">Client</option>
<option value="22">ABC</option>
<option value="7">ABC1</option>
<option value="18">ABC2</option>
<option value="27">ABC3</option>
<option value="26">ABC4</option>
<option value="31">ABC5</option>
<option value="12">ABC6</option>
<option value="19">ABC7</option>
<option value="72">DGX Client</option>
<option value="57">DS Sampler</option>
<option value="25">Group123</option>
</select>

我在 Selenium Webdriver 中编写的获取值的代码:

@FindBy(id="ClientId")
WebElement clientDropDown;
waitTime = new WebDriverWait(driver,20);
waitTime.until(ExpectedConditions.visibilityOf(clientDropDown));
Select client=new Select(clientDropDown);
client.selectByVisibleText("DGX Client");

错误:org.openqa.selenium.TimeoutException:预期条件失败:等待可见性 [[ChromeDriver: chrome on XP (6fa8cbb25476bea9b789aff19a6edf)] -> id: ClientId](尝试 30 秒,间隔 500 毫秒)

【问题讨论】:

  • 有什么症状?运行代码时会发生什么?
  • org.openqa.selenium.TimeoutException: 预期条件失败:等待可见性 [[ChromeDriver: chrome on XP (6fa8cbb25476bea9b789aff19a6edf)] -> id: ClientId] (尝试了 30 秒) 500 毫秒间隔)
  • 在添加 WebDriverwait 语句之前,我收到了这个错误:org.openqa.selenium.ElementNotVisibleException: element not visible: element is not currently visible and may not be mapped

标签: java selenium selenium-webdriver


【解决方案1】:

在此假设下运行。

看看select 是如何被display: none 样式隐藏的:

<select ... style="display: none;">
            ^^^^^^^^^^^^^^^^^^^^^

我认为这是因为它实际上在 UI 上以不同的方式表示,并且只要实际的下拉表示发生变化,这个 select 就会在后台由 javascript 进行操作。

如果是这种情况,您可以检查实际下拉列表的外观并使用click() 命令的组合打开下拉列表并选择所需选项(请注意,您将无法使用Select在这种情况下类,因为它被设计为仅用于 select 元素)。

或者,您可以使select 元素可见并继续

String js = "arguments[0].style.display='block'"; 
((JavascriptExecutor) driver).executeScript(js, clientDropDown);

Select client = new Select(clientDropDown);
client.selectByVisibleText("DGX Client");

【讨论】:

  • 非常感谢..它就像一个魅力。代码运行顺利,没有任何错误。再次感谢:)
  • btw..我只使用了这两行代码: String js = "arguments[0].style.display='block'"; ((JavascriptExecutor) 驱动程序).executeScript(js, clientDropDown);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-15
  • 1970-01-01
  • 1970-01-01
  • 2015-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多