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>
Bharat Mane
6,78512 gold badges42 silver badges69 bronze badges
asked May 11, 2017 at 10:02
3
  • <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> Commented May 11, 2017 at 10:03
  • Please properly format the HTML code and put in into original post. Commented Oct 8, 2017 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. Commented Nov 8, 2017 at 6:36

5 Answers 5

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.

Rishi
8955 silver badges24 bronze badges
answered May 7, 2018 at 11:56
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);
answered May 22, 2018 at 11:04
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')] 
Bharat Mane
6,78512 gold badges42 silver badges69 bronze badges
answered May 11, 2017 at 11:46
0
0

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

It will work as expected

Bharat Mane
6,78512 gold badges42 silver badges69 bronze badges
answered Oct 9, 2017 at 5:58
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; 
 }
answered Dec 8, 2017 at 9:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.