Getting org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been “select” but was “div”

1,880 views
Skip to first unread message

Snow Gift

unread,
Feb 15, 2020, 12:17:47 AM2/15/20
to Selenium Users

When I'm trying to get the Dropdown options I am getting an Exception.


Exception

org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "div"

Source Code

driver.findElement(By.xpath("(//a[@class='navbar-nav-link dropdown-toggle'])[1]")).click();
Select drp = new Select (driver.findElement(By.xpath("//div[@class='dropdown-menu show']")));
    List<WebElement> opt = drp.getOptions();
    for (WebElement we : opt) {
        String name = we.getText();

HTML Code

<a href="#" class="navbar-nav-link dropdown-toggle" data-toggle="dropdown" aria-expanded="true"><i class="fa fa-paper-plane"></i> <span>Flight</span></a>
  <div class="dropdown-menu show">
     <a href="AirHistory" class="dropdown-item"><i class="fa fa-history"></i> <span>History Booking</span> </a>
     <a href="AirRetrievePNR" class="dropdown-item"><i class="fa fa-download"></i> <span>Import PNR</span> </a>
     <a href="AirReschedule" class="dropdown-item" style="display:none"><i class="fa fa-exchange"></i> <span>Online ReIssue</span> </a>
  </div>

I can't able to Select or Access the Dropdown...


Help me to get out of this..!!

sagar yenkar

unread,
Feb 15, 2020, 1:22:41 AM2/15/20
to seleniu...@googlegroups.com
Try this one 
Select size = new Select(driver.findElement(By.xpath("")));
        size.selectByVisibleText("100");
        size.selectByIndex(5);

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/b1bc8d54-2868-411d-9da1-a2c78902b0c8%40googlegroups.com.
Message has been deleted

Snow Gift

unread,
Feb 15, 2020, 2:29:31 AM2/15/20
to Selenium Users
Again I got an Exception......

org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "div"

Dinesh K

unread,
Feb 15, 2020, 2:43:47 AM2/15/20
to Selenium Users
I think it's a jQuery's custom dropdown, you have to write your own custom method to select the value/text. 

How to identify & select value in dropdown using 3 steps approach:

1. First Identify & Click the link element - "navbar-nav-link".
2. Wait for the List of WebElements to be displayed - //div[@class='dropdown-menu show']/a.
3. Now find & select the value.

Please refer the below link will give an idea about how to approach it

http://learn-automation.com/handle-bootstrap-dropdown-in-selenium-webdriver/

Hope this helps !

Regards,
Dinesh K

Debkumar Das

unread,
Feb 15, 2020, 6:15:56 AM2/15/20
to seleniu...@googlegroups.com
Hey,

May be in html they did not used Select tag. Try click on drop-down and then get all the values in List and try to access.

--

mrinmoy saha

unread,
Feb 15, 2020, 10:49:20 PM2/15/20
to Selenium Users
Hi,

Can u share the link of the webpage?

Are u trying to access all the drop-down list values and print them?

Thanks,
Mrinmoy

Bathiy L.

unread,
Feb 16, 2020, 7:34:46 AM2/16/20
to Selenium Users
As per the given HTML code, you cannot construct a Select since tag of the element is not Select (not standard select/dropdown html element),
To get options from this you can try something as below

     List<WebElement> opt = driver().findElements((By.xpath("//div[@class='dropdown-menu show']/a")));
    for(WebElement we : opt) {
    String name = we.getText();
    }


On Saturday, February 15, 2020 at 10:47:47 AM UTC+5:30, Snow Gift wrote:

vikas sv

unread,
Feb 17, 2020, 7:29:03 AM2/17/20
to Selenium Users
You cannot use Select option for a div, that is why you get the exception.

you can use Click option only on combo boxes of type div.

Best Regards,
Vikas
Reply all
Reply to author
Forward
0 new messages