I tried this thing and it works perfectly when the tag name is selected, but when I use Select select = new Select();
in the tag name div
, or something, it fails to work and prompts me with the following error: org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "div"
.
<div stop-propagation="click" class="custom-select-search">
<input class="undefined ng-pristine ng-valid ng-touched" type="text" autocomplete="off" ng-model="searchTerm">
</div>
<ul role="menu">
<li role="presentation" class="ng-scope">
<a role="menuitem" tabindex="-1" href=""ng-click="select(s)" class="ng-binding">166</a>
</li>
<li role="presentation" class="ng-scope">
<a role="menuitem" tabindex="-1" href=""ng-click="select(s)" class="ng-binding">164</a>
</li>
<li role="presentation" class="ng-scope">
<a role="menuitem" tabindex="-1" href=""ng-click="select(s)" class="ng-binding">165</a>
</li>
<li role="presentation" class="ng-scope">
<a role="menuitem" tabindex="-1" href=""ng-click="select(s)" class="ng-binding">166</a>
</li>
</ul>
This is my code:
WebElement elements = driver.findElement(serial);
Select ComboBoxValues = new Select(elements);
List <WebElement> weblist = ComboBoxValues.getOptions();
int iCnt = weblist.size();
Random num = new Random();
int iSelect = num.nextInt(iCnt);
ComboBoxValues.selectByIndex(iSelect);
System.out.println("Element Name 1 : " +elements.getAttribute("value"));
Can anyone tell me to do this.
As per selenium select documentation,
***Open Declaration org.openqa.selenium.support.ui.Select
Models a SELECT tag, providing helper methods to select and deselect options.
which mean SELECT can be used for drop-downs with select tag only . You applications drop-downs are made using DIV tag. that's the reason your code doesn't works on your web page
Looks like the site has some custom UI that emulates a dropdown. Which means you'll have to implement your own code to interact with it. I would recommend creating a class, let's call it MySiteSelector, that will have a select() method which knows where to click to open the menu list (it doesn't look like that's here), then will search through your list items (a css selector would be "ul[role='menu] li") to find the one that matches your desired target.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.