I need to select a value from drop-down, which has javascript. Any help is highly appreciated!
Following HTML:
<div class="popupMenuButton">
<a id="_sawrl" bh="PML" _mid="_g7yog" class="awmenuLink" style="text-decoration:none" href="#">
<nobr>
FORMALIN VAPO TABS
<img width="15" height="17" align="absmiddle" border="0" style="margin-bottom:1px;" src="/EasyCare-2.0/AribaWeb/ad/content/AWXDebugResourceActions/13/en_US/widg/arrowcolor.gif" alt="">
</nobr>
</a>
</div>
Java code
WebElement dropDownListBox = waitById("_sawrl");
Select clickThis = new Select(dropDownListBox);
clickThis.selectByVisibleText("FORMALIN VAPO TABS")
Exception
Exception in thread "main" org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "a"
Build info: version: '2.42.2', revision: '6a6995d', time: '2014-06-03 17:42:30'
System info: host: , os.name: 'Windows 8', os.arch: 'x86', os.version: '6.2', java.version: '1.7.0_51'
Driver info: driver.version: unknown
at org.openqa.selenium.support.ui.Select.<init>(Select.java:46)
at ui.stores.UIMaterialRecepit.setBrandName(UIMaterialRecepit.java:71)
at tc.stores.TCMaterialRecepit.receiveMaterial(TCMaterialRecepit.java:32)
at tc.StartAutmation.main(StartAutmation.java:48)
3 Answers 3
As the exception says:
Element should have been "select" but was "a"
.
Your dropDownListBox
is an anchor element. (<a id="_sawrl"...
).
new Select()
only accepts <select>
elements.
4 Comments
As said by Cerbrus, your element is not a real select. You can handle such stuff by clicking.
Say, your 'select' looks like:
<div class="menubox-select AFGFLKJV">
<div class="menuItem" row="1" >Info</div>
<div class="menuItem" row="2" >Stuff</div>
<div class="menuItem" row="3" >Teddy Bears</div>
</div>
Then, in order to select:
driver.findElement(By.xpath("//div[contains(@class, 'menubox-select')]").findElement(By.xpath(".//*[contains(text(), 'Stuff')]").click();
That's quite a usual thing for custom selectboxes. You can, however, omit using element.findElement. Still before clicking an option you need to trigger options to be visible, otherwise you get exception.
Comments
I was able to figure out the problem. Thank you @Cerbrus and @TEH for the pointers.
Following is the java code:
driver.findElement(By.xpath("//*[@id='_sawrl']").click();
driver.findElement(By.xpath("//*[@id='_immwib']").click();
Comments
Explore related questions
See similar questions with these tags.