java 从 div 类下拉列表中选择 - Selenium
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34770848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selecting from div class dropdown - Selenium
提问by NewCoder888
I'm trying to select an option from a drop-down that doesnt populate until the locator has been clicked. This is what I see in Firebug:
我正在尝试从下拉列表中选择一个选项,该选项在单击定位器之前不会填充。这是我在 Firebug 中看到的:
div class="selectize-input items not-full has-options">
<input type="text" autocomplete="off" tabindex="" placeholder="523-23-XXXXX" style="width: 109px; opacity: 1; position: relative; left: 0px;">
</div>
<div class="selectize-dropdown multi form-control" style="display: none; width: 263px; top: 34px; left: 0px; visibility: visible;">
<div class="selectize-dropdown-content">
<div class="option" data-selectable="" data-value="523-23-20273">523-23-20273</div>
<div class="option" data-selectable="" data-value="523-23-20274">523-23-20274</div>
<div class="option" data-selectable="" data-value="523-23-20275">523-23-20275</div>
<div class="option" data-selectable="" data-value="523-23-20276">523-23-20276</div>
<div class="option" data-selectable="" data-value="523-23-20280">523-23-20280</div>
<div class="option" data-selectable="" data-value="523-23-202801">523-23-202801</div>
The code I have so far is:
我到目前为止的代码是:
public void selectAgentCodes(String agentCode)
{
driver.findElement(byAgentCodes).click();
new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(By.className("selectize-dropdown-content")));
Select select = new Select(driver.findElement(By.className("selectize-dropwodn-content")));
select.selectByVisibleText(agentCode);
}
I get an UnexpectedTagNameException: Element should have been "select" but was "div". I'm not sure how to handle this as I've only worked with selects before.
我得到一个UnexpectedTagNameException: Element should have been "select" but was "div"。我不确定如何处理这个问题,因为我之前只使用过选择。
Lets say I wanted to select "523-23-20275" for the agent code. How would I go about this?
假设我想为代理代码选择“523-23-20275”。我该怎么办?
Any help is appreciated! Thanks.
任何帮助表示赞赏!谢谢。
回答by JRodDynamite
This isn't a normal drop-down select menu. Hence, using Select
won't work in this case. Without seeing the complete site, I'm not sure what exactly must be done to select it.
这不是一个普通的下拉选择菜单。因此,Select
在这种情况下使用将不起作用。没有看到完整的网站,我不确定必须做什么才能选择它。
But try simply clicking on the div
element when the options in the dropdown are visible.
但是,div
当下拉列表中的选项可见时,请尝试简单地单击该元素。
//I'm assuming that this will display the dropdown list
driver.findElement(byAgentCodes).click();
driver.findElement(By.xpath("//div[@data-value='523-23-20275']"));
回答by RAZA A
Here, Select class wouldn't work if there is no select tag in the UI, you need to click on the main div, and then you need to click on any of the div having multiple options, it will first click on the drop down and then click on the specific element from the list, below code will hopefully work for you....
这里,如果 UI 中没有 select 标签,Select 类将不起作用,您需要单击主 div,然后需要单击具有多个选项的 div 中的任何一个,它会首先单击下拉列表然后单击列表中的特定元素,下面的代码希望对您有用....
1)firstly you need to click on this div by finding it through any of the available methods like by id, xpath, css selector, driver.findElement(byAgentCodes).click(); clicking on this will open a dropdown list
1)首先,您需要通过任何可用的方法(如通过 id、xpath、css 选择器、driver.findElement(byAgentCodes).click() 找到它来单击此 div;单击此按钮将打开一个下拉列表
2) repeat the same above point 1 for clicking the any of list items in dropdown
2) 重复上述第 1 点以单击下拉列表中的任何列表项
523-23-20275That will work.
那可行。
回答by Sumit Garg
Follow the below steps to select an item under div tag:
按照以下步骤选择 div 标签下的项目:
You have to use collections object to store all child elements which are stored under same tag. For Ex if you have below HTML structure:
您必须使用集合对象来存储存储在同一标签下的所有子元素。对于 Ex,如果您有以下 HTML 结构:
<div id="Year">
<div class="abc">
<ul class = "xyz">
<li id=1>2000</li>
<li id=2>2001</li>
<li id=3>2002</li>
<li id=4>2003</li>
<li id=5>2004</li>
</ul>
</div>
</div>
Write below selenium code:
写下面的硒代码:
List<Webelement> lst = driver.findElements(By.xpath(<locator of child elements>));
//In this case it is //div[@id='Year']/div/ul/li
System will store all child elements in the list then you can select any element by index method using
系统会将所有子元素存储在列表中,然后您可以使用 index 方法选择任何元素
lst.get(<index value>).click();
if you do not want to find using index but text use Iterator interface to find the element from the collection then click on that element:
如果您不想使用索引查找,但文本使用 Iterator 接口从集合中查找元素,然后单击该元素:
Iterator<Webelement> it = lst.iterator();
while (it.hasNext()) {
WebElement wb = it.next();
if(wb.getText().equals(<Text to find in double quotes>)) {
wb.click();
break;
}
}