简体   繁体   English

无法从Selenium和Java中的索引下拉列表中选择

[英]Unable to Select From Dropdown By Index in Selenium and Java

I understand how to Select an element from dropdown using selectByIndex method. 我了解如何使用selectByIndex方法从下拉菜单中选择元素。 Howeverwhen I try to perform selectByIndex ( select.selectByIndex (index) ) on the following HTML snippet: 但是,当我尝试在以下HTML代码段中执行selectByIndex( select.selectByIndex (index) )时:

<select id="destinationAllocationId" name="destinationAllocationId">
<optgroup label="Anywhere Virtual Cards">
<option value="1555555555f0a19">NNN0 [*-2453]</option>
<option value="1555555555f0519">NNN1 [*-8354]</option>
<option value="155555555550419">NNN2 [*-5765]</option>
<option value="155555555520919">NNN3 [*-8875]</option>
</optgroup>
</select>
</div>

I'm unable to maintain the selected option. 我无法维持所选的选项。

At first it appears to work (the dropdown menu opens and the correct option is highlighted inblue). 一开始它似乎可以工作(下拉菜单打开,正确的选项以蓝色突出显示)。 However as soon as the program continues with execution (something unrelated to dropdown) the dro pdown reverts back to the original state (the default option). 但是,一旦程序继续执行(与下拉菜单无关),则dropdown会恢复为原始状态(默认选项)。

In other words, initially the the program selects the appropriate option just fine but it automatically reverts back to the default option and doesn't actually change the value in the dropdown once the dropdown menu disappears (Usually when I select an option manually I can see that option being selected once the drop down is closed however in this case as long as dropdown is open the option is selected but as soon as it closes the default option is selected) 换句话说,程序最初会选择适当的选项,但是它会自动恢复为默认选项,并且在下拉菜单消失后实际上不会更改下拉菜单中的值(通常,当我手动选择一个选项时,下拉菜单关闭后即会选择该选项,但是在这种情况下,只要下拉菜单处于打开状态,就会选择该选项,但关闭后会立即选择默认选项)

Any ideas why this happens 任何想法为什么会这样

i had this issue, too 我也有这个问题

this is how i solved it 这就是我解决的方法

/**
 * helper method for handling drop downs
 * @param idPrefix the id of the drop down without any _xy
 * @param value the value to be selected
 */
public void clickDropdown(String idPrefix, String value) {
    // find & click on the label element of the drop down
    firefoxDriver.findElement(By.id(idPrefix + "_label")).click();
    // find & choose element from drop down
    firefoxDriver.findElement(By.xpath("//div[@id='" + idPrefix + "_panel']/div/ul/li[text()='" + value + "']")).click();
}

u call it like this: 你这样称呼它:

// choosing an element in the drop down menu
clickDropdown("projectForm:stellvertreter", "StringToSelect");

this i wrote for use with primefaces, you have to modify it anyway but u get the idea 我写的是与primefaces一起使用的,您无论如何都必须对其进行修改,但是您知道

for the issue with selecting and reverting... try sending an enter key press to the selected item or clicking it again... didnt have that with my approach 有关选择和还原的问题...尝试将Enter键发送到所选项目或再次单击它...我的方法没有

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Selenium Webdriver JAVA-无法从下拉菜单中选择选项(JAVA) - Selenium Webdriver JAVA - Unable to select option from dropdown menu (JAVA) 无法在Java Selenium中选择Bootstrap下拉列表 - Unable to select Bootstrap dropdown in Java Selenium Eclipse Java Selenium Webdriver-无法选择下拉项(ElementNotVisibleException) - Eclipse Java Selenium webdriver - Unable to select dropdown item (ElementNotVisibleException) 无法使用Selenium Java在iframe中选择下拉菜单 - Unable to select dropdown inside iframe using selenium java 无法从Selenium Webdriver的下拉列表中选择项目 - Unable to select item from dropdown list in Selenium Webdriver 无法从下拉菜单中选择 - Unable to select from dropdown 如何使用Selenium WebDriver和java从下拉列表中选择项目? - How to select an item from a dropdown list using Selenium WebDriver with java? 如何 select Selenium Java 中的列表中的选项(不在下拉列表中) - How to select an option from a list (not in dropdown) in Selenium Java 如何使用 selenium webdriver 和 Java 从剑道下拉菜单中选择一个选项 - How to select an option from the kendo dropdown using selenium webdriver and Java 如何根据Selenium和Java的HTML从下拉菜单中选择一个选项 - How to select an option from the dropdown as per the HTML through Selenium and Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM