1

I need to select a value from dropdown can some one help me on that. Html part is below

<div id="element11_chzn" class="chzn-container chzn-container-single" style="width: 320px;">
<a class="chzn-single" href="javascript:void(0)" tabindex="0">
<div class="chzn-drop" style="left: -9000px; width: 318px; top: 29px;">
<div class="chzn-search">`enter code here`
<ul class="chzn-results">
<li id="element11_chzn_o_1" class="active-result" style="">ActiveLearn Course</li>
<li id="element11_chzn_o_2" class="active-result" style="">ActiveLearn Player</li>
<li id="element11_chzn_o_3" class="active-result result-selected" style="">ActiveLearn Skin</li>
<li id="element11_chzn_o_4" class="active-result" style="">ActiveLearn Template</li>
<li id="element11_chzn_o_5" class="active-result" style="">Activity</li>
<li id="element11_chzn_o_6" class="active-result" style="">Animation</li>
<li id="element11_chzn_o_7" class="active-result" style="">Assessment</li>
<li id="element11_chzn_o_8" class="active-result" style="">Bookmarks</li>
<li id="element11_chzn_o_9" class="active-result" style="">Character</li>
<li id="element11_chzn_o_10" class="active-result" style="">Click to prompt</li>
<li id="element11_chzn_o_11" class="active-result" style="">Click to prompt override</li>
<li id="element11_chzn_o_12" class="active-result" style="">EBook</li>
<li id="element11_chzn_o_13" class="active-result" style="">Exercise</li>
<li id="element11_chzn_o_14" class="active-result" style="">Game</li>
<li id="element11_chzn_o_15" class="active-result" style="">Glossary</li>
<li id="element11_chzn_o_16" class="active-result" style="">Glossary Term</li>
<li id="element11_chzn_o_17" class="active-result" style="">Glossary Term</li>
<li id="element11_chzn_o_18" class="active-result" style="">Imported file</li>
<li id="element11_chzn_o_19" class="active-result" style="">Interactive activity</li>
<li id="element11_chzn_o_20" class="active-result" style="">Interactive page</li>

</ul>
</div>

I have to make it dynamic so I need to get the value from xls.

3

6 Answers 6

4

This should work for you :

Select select = new Select(yourDropdownElement);
select.selectByVisibleText(text);
Sign up to request clarification or add additional context in comments.

2 Comments

This won't work, because it is not a <input type="select" button>
I have got this error org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: Element cannot be scrolled into view:[object HTMLInputElement]
4

Since you are not using actually a element, i'd use the following code that should suit perfectly in that scenario. That should click in case a element finds the correct text inside the <li> element.

public void selectValueFromUnorderedList(WebElement unorderedList, final String value) {
    List<WebElement> options = unorderedList.findElements(By.tagName("li"));

    for (WebElement option : options) {
        if (value.equals(option.getText())) {
            option.click();
            break;
        }
    }
}

To use this method, all you need to do send the proper WebElement and the String you're looking to find. Say you need to get Game, in your scenario that's easy as:

WebElement ul = driver.findElement(By.className("chzn-results"));
selectValueFromUnorderedList(ul, "Game");

And, voilà! Hope it helps.

Comments

1
var ul = document.getElementById("chzn-results");
var liArray = ul.getElementsByTagName("li");

for (var i = 0; i < liArray.length; i++) {
     {
        //where liArray[i] being the LI element on the position (i) ;
    }
}

Comments

1
driver.findElement(By.id("element11_chzn")).click();
driver.findElement(By.id("element11_chzn_o_7")).click(); /*It will select 'Assessment' from drop down.*/

Hope it will help you.

Comments

0

Sample program to get value from drop down :

public class demo {


       public static void main(String[] args) throws IOException, InterruptedException {


       FirefoxDriver driver = new FirefoxDriver();

        //OPEN SPECIFIC URL IN BROWSER
        driver.get("http://www.toolsqa.com/automation-practice-form/");


       //SELECT SPECIFIC VALUE FROM DROPDOWN
       Select sel = new Select(driver.findElement(By.id("continents")));
       sel.selectByVisibleText("Australia");

        }
   }

1 Comment

Please note that we expect answers to address the specific issues in the question. If you have a very relevant alternative, you can consider adding that - but please be sure to address the question and explain exactly why, first.
0

Sample statements to open browser, load URL and select value from dropdown

static WebDriver driver;
System.setProperty("webdriver.ie.driver","C:\\(Path)\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
driver.manage().window().maximize();

driver.get("EnterURLHere");          
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);

Select value1 = new Select(driver.findElement(By.id("element11_chzn")));    
value1.selectByVisibleText("Character");    //Select Character from dropdown list

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.