2

I am new to automated testing with Selenium Web Driver I am not able to work out how to test drop down lists without using the select command.

I tried using xpath, select by id, class name, css selector, but the drop down list does not open or select the child item in the list.

Please provide me with code samples for this.

My code :

driver.findElement(By.cssSelector("html body.modal-open.page-overflow div.modal-scrollable div#responsive.modal.hide.fade.modal-overflow.in div#update_data1.modal-body form#finv div#rootwizard3 div.tab-content div#tab1.tab-pane.no_padding.active div.form_row div.field div#dn_chzn.chzn-container.chzn-container-single a.chzn-single span")).submit();
            Select drpname = new  Select(driver.findElement(By.xpath("/html/body/div[4]/div/div[2]/form/div/div[3]/div[1]/div/div/div/a")));
            driver.findElements(By.xpath("/html/body/div[4]/div/div[2]/form/div/div[3]/div[1]/div/div/div/a/span=1001 (2015-04-29 10:49:11)"));

    drpname.getOptions();
    drpname.getFirstSelectedOption();
    drpname.selectByIndex(1);
    drpname.selectByVisibleText("1000 (2015-04-29 10:47:45)");
    drpname.selectByValue("1000 (2015-04-29 10:47:45)");

The HTML I am trying to test is here:

http://i.stack.imgur.com/yjoe2.png


closed as unclear what you're asking by alecxe Sep 13 '17 at 13:56

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Possibly part of your problem, but a css selector that long is prone to be very brittle. If any element you're using is out of place then your desired element will not be able to be located. Think about how you can locate your desired element while using as few other elements as possible as anchors. For example, if there is no other element with the class "chzn-single" then you could use the css selector "a.chzn-single span" – Julian Oct 30 '15 at 17:34
0

This doesn't appear to be a traditional select list. Based on a very similar jQuery dropdown I believe you will first want to click the below element

<div id="dn_chzn"....>

You can do this with the simple locater below

driver.findElement(By.id("dn_chzn")).click()

At this point the dropdown should be open. The list of options is actually nested in the element I list below, which unfortunately is not expanded in the screenshot.

<div class="chzn-drop"....>

Once more based on a similar jQuery plugin, an xpath query you could use I have listed below.

//div[@class='chzn-drop']//li[contains(text(),'1001 (2015-04-29 10:49:11)')]

Replace the dropdown selection text in the contains() portion of the query with the text of the element you would like to select.

0

I think the issue here because your <select> has style="display: none;". In this case, I think you should click on the div first and try to Select option.

Below is my code:

WebElement divClick = driver.findElement("//div[@class='form_row']/div");
divClick.click();

WebElement dropdown = driver.findElement("//select[@id='dn']");
Select select = new Select(dropdown);
select.selectByVisibleText("your text here");

Try it first. Hope it helps for you. If there's any error, please let me know. When trying to find element, try using the WebDriverWait like :

dynamicElement = (new WebDriverWait(driver, second))
                 .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("your css here"));
0

My product use AngularJS and Select2 jquery for the dropdown and it appears same as your case, hope my exeperience can help you

There are 2 ways to do

  1. Execute JS to enable the dropdown, then select item as normal

    driver.ExecuteScript($('elementLocator').attr('Style','display:block'))

  2. Click on div first to enable something (as @scorpion mentioned) , then sendkeys to text field (in my case) or select from dropdown if the drop down is enabled now.

0

Executing a javascript click on the option from the dropdown should work, because for a javascript click to work elementToClicked should be present on the dom (it does not matter if its display is none or not).

Snippet (java):

((JavascriptExecutor)driver).executeScript("arguments[0].click;", driver.findElement(By.cssSelector("optionFromDropdown")));
  • This would also work if the drop down doesn't and therefor isn't really testing user experience. A purely gui based approach would probably be more effective from a QA point of view. – Daniel Aug 23 '17 at 12:51

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