Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.5k views
in Technique[技术] by (71.8m points)

visual studio - OpenQA.Selenium.Support.UI.UnexpectedTagNameException : Element should have been select but was "title" c#

I'm trying to access to the permissions section in the page i'm working for, for a test in selenium, but i don't know how to handle that element, i guess is a dropdown menu, so i make a code like this (I'm using selenium POM btw):

[FindsBy(How = How.CssSelector, Using =".dir")]
private IWebElement elemPermi;

    public  Permissions selectElementFromDropdown( string text)
    {
        SelectElement drop = new SelectElement(elemPermi);
        drop.SelectByText(text);
        return new Permissions(driver);
    }

And in the test i have this:

UsuarioMain usuario = new UsuarioMain(driver);
usuario.selectElementFromDropdown("Modules");

This is the HTML code of the page:

<div id="mainmenu">
    <ul id="nav" class="dropdown dropdown-horizontal">
        <li class="normal"><a href="/TravelTendering/" class="selected">Home</a></li>
        <li class="normal"><a href="/TravelTendering/Configuration">Configurations</a></li>
        <li class="dir">Permissions<div class="separadorMenu"></div>
            <ul class="dropdownMenu"><li><a href="/TravelTendering/Profile">Profiles</a></li>
            <li><a href="/TravelTendering/Role">Roles</a></li>
            <li><a href="/TravelTendering/Permission">Modules</a></li>
            <li><a href="/TravelTendering/User">Users</a></li>
            </ul>
        </li>
        <li class="dir">Tenders<div class="separadorMenu"></div>
            <ul class="dropdownMenu"><li><a href="/TravelTendering/Tender">Create Tender</a></li>
            <li><a href="/TravelTendering/SeeTender">See Tenders</a></li>
            </ul>
        </li>
            <li class="normal"><a href="/TravelTendering/Supplier">Invitations</a></li>
            <div style="clear:both; float:none;"></div>
    </ul>
</div>

But when i run my test this error appears:

Message: OpenQA.Selenium.Support.UI.UnexpectedTagNameException : Element should have been select but was li

I want to be able to interact with any element of the dropdown, so if you guys can help me, i will appreciate it a lot.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
SelectElement drop = new SelectElement(elemPermi);

you can use SelectElement class with select tag only , for other tag you have use normal webelement interaction like

  1. Click dropdown menu
  2. Wait till all options are displayed
  3. Click on any specific option using index or text ( using li[2] or li[contains(text(),"blabla")]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...