无法从下拉列表中选择值

Posted

技术标签:

【中文标题】无法从下拉列表中选择值【英文标题】:Unable to select the value from the drop list 【发布时间】:2016-04-14 04:35:51 【问题描述】:

我知道问题对你们来说很容易回答,但是我卡在一个下拉列表中,我想从中选择出生月份。

我一直在其他网站上工作,但不幸的是无法使其正常工作。

我试过了:

点击使用不同的定位器。 使用 Action 类移动悬停和单击。 使用 Java Script 选择月份。

奇怪的是,如果我得到 WebElement 的文本,我可以这样做,但无法点击它。

应用程序的网址是:a link!

这是在谷歌上创建帐户时。

我试图从任何月份的列表中选择月份。

当我等到盒子有所需的月份时,我得到了 TimeOutException。

除此之外,我没有收到任何错误。脚本总是通过而不进行选择。

我知道这个问题很简单,但是如果有人可以帮助我解决它。

提前致谢。

<span id="BirthMonth" class=" form-error" aria-invalid="true">
<div class="goog-inline-block goog-flat-menu-button jfk-select" role="listbox" style="-moz-user-select: none;" aria-expanded="false" tabindex="0" aria-haspopup="true" aria-activedescendant=":0" title="Birthday">
<div class="goog-menu goog-menu-vertical" style="-moz-user-select: none; visibility: visible; left: 0px; top: -82.2333px; display: none;" role="listbox" aria-haspopup="true">
<div id=":1" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div id=":2" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div class="goog-menuitem-content">February</div>
</div>
<div id=":3" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div id=":4" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div id=":5" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div id=":6" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div id=":7" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div id=":8" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div id=":9" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div id=":a" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div id=":b" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div id=":c" class="goog-menuitem" role="option" style="-moz-user-select: none;">
</div>
<input id="HiddenBirthMonth" type="hidden" name="BirthMonth">
</span>

【问题讨论】:

请在问题中添加相关的html 请看添加的html。或者,可以通过帖子中的链接找到它。它是一般的 gmail 帐户网页。 【参考方案1】:

您是否尝试过使用 sendkey?我希望通过使用 sendkey 它会工作

driver.findElement(By.xpath(".//*[@id='BirthMonth']/div[1]")).sendKeys("april");

谢谢

【讨论】:

感谢您的回答 Murali。但不幸的是,它只是从列表中悬停到那个月份,但不要选择它。 我试过这个并且工作正常..给我你试过的代码..我刚刚得到那个注册页面并使用了这个sendkeys..它选择了月份。我不确定你是如何尝试的。请提供您的代码 public void selectMonthFromMonthList()birthMonthDropList.click(); wait.until(ExpectedConditions.visibilityOfAllElements(months)); driver.findElement(By.xpath(".//*[@id='BirthMonth']/div[1]")).sendKeys("february"); 这只是将鼠标悬停到选定的月份,而不是实际点击月份。 请使用它而不点击那个月份的下拉菜单。谢谢【参考方案2】:

请尝试以下代码。它必须工作。

Select(driver.findElement(By.id("BirthMonth"))).selectByVisibleText("February");

如果没有,请先点击下拉列表。然后将下拉列表中的所有值存储到数组或列表中,然后从列表/数组中选择所需的值。

【讨论】:

嗨 Shiva,感谢您的回复,但不幸的是我尝试了所有这些,但它不起作用。循环列表时,我可以打印标签的文本,但无法选择它们。 我可以使用上面的代码在 Safari 浏览器中选择月份。但无法在Firefox中。【参考方案3】:

您可以先单击BirthMonth 打开下拉菜单,然后单击所需的月份。选择六月见下文:

driver.findElement(By.cssSelector("#BirthMonth > div")).click();
driver.findElement(By.xpath("//*[contains(text(),'June')]")).click();

【讨论】:

感谢您的回答。这也行不通。它给了我以下错误:rg.openqa.selenium.WebDriverException: unknown error: Element is not clickable at point (1154, 524)。其他元素会收到点击:... (Session info: chrome=47.0.2526.106) (驱动程序信息:chromedriver=2.19.346078 (6f1f0cde889532d48ce8242342d0b84f94b114a1),platform=Windows NT 6.3 x86_64)(警告:服务器未提供任何堆栈跟踪信息) 嗨 Mesut,我使用的是 chrome 和 firefox 浏览器。

以上是关于无法从下拉列表中选择值的主要内容,如果未能解决你的问题,请参考以下文章

从下拉列表中选择值,第二个下拉列表自动更改

在“选择”/下拉 HTML 列表中手动输入值?

如何从搜索下拉列表中选择值

从 Google 电子表格的下拉列表中选择多个值

如何检查是否从HTML下拉列表中选择了某个项目?

如何通过从另一个下拉列表中选择值来填充下拉列表?