0

I'm trying to select an option from dropdown with one option using the below code; but I'm getting an error message that reads "Element should have been "select" but was "li"

CODE:

WebElement ERA = driver.findElement(By.id("Menu_8990"));

Select dropdown = new Select(ERA);      
dropdown.selectByVisibleText("ERA Registration");

HTML:

<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true"><span class=""></span>&nbsp;&nbsp;ERA <span class="caret"></span></a> 

<a href="javascript:void(0)" onclick="submitting_upload_main('172.17.8.53/ERA/registratio‌​n_uploaded');">ERA Registration</a>
  • <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true"><span class=""></span>&nbsp;&nbsp;ERA <span class="caret"></span></a> <a href="javascript:void(0)" onclick="submitting_upload_main('172.17.8.53/ERA/registration_uploaded');">ERA Registration</a> – Thabiso Ditabe May 11 '17 at 10:03
  • Please properly format the HTML code and put in into original post. – Jitesh Sojitra Oct 8 '17 at 17:32
  • Selenium expect only select and option tags for select class. For other type dropdown you need to use simple way to select element. Click on the dropdown and click on the option is simplest way. – Sagar007 Nov 8 '17 at 6:36
3

You may use the Actions for this.

Since the HTML code does not have Select class, you cannot use the Select statement for this dropdown list.

The HTML code has Span class, so it will be easier to use Actions for this object than Select.

Actions drpdwn = new Actions(driver);
driver. findElement(By.xpath("XPATH OF DROPDOWN FIELD")).click();
Action selectobject = drpdwn.movetoElement(findElement(By.linkText("Object name")).click().build();
selectobject.perform();

Using this code may help you to select the object in the drop-down which does not have a Select Class.

2

You can use simple click & enter for the first drop-down element selection.

driver.findElement(By.id("DROP-DOWN FIELD XPATH")).click();
driver.findElement(By.xpath("FIRST ELEMENT XPATH")).click();
driver.findElement(By.xpath("FIRST ELEMENT XPATH")).sendKeys(Keys.ENTER);
0

I worked on this sort of code in my software QA company.

The Select function works only if there is a select element in HTML. Further, you can directly select the dropdown option by creating below XPath and clicking directly after opening the dropdown.

//a[contains(text(),'ERA Registration')] 
0

You can locate the elements through linkText command.
Eg: driver.findElement(By.linkText("ERA Registration"));

It will work as expected

0

You may create a method like:

private YourPageObject selectFromDropdown(String whatIwantToSelect)
    {
        new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOf(By.cssselector("a[class=\"dropdown-toggle\"]"))).click();
        Select select = new Select(By.cssselector("a[class=\"dropdown-toggle\"]"));
        select.selectByVisibleText(whatIwantToSelect);
        return this;    
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.