0

My Html

<div id="example-1-tab-2" class="responsive-tabs-panel" style="display: none;">
    <div class="freme_box">
    <iframe class="demo-frame" src="dropdown/default1.html">
        <html lang="en">
           <head>
           </head>
           <body>
               <div class="ui-widget">
                   <select id="combobox" style="display: none;">
                       <option selected="" value="Please Enter">Please Enter</option>
                       <option value="Afghanistan">Afghanistan</option>
                       <option value="Albania">Albania</option>
                   </select>
                   <span class="custom-combobox">
                       <input class="custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left ui-autocomplete-input" title="" autocomplete="off">
                       <a class="ui-button ui-widget ui-state-default ui-button-icon-only custom-combobox-toggle ui-corner-right" tabindex="-1" title="Show All Items" role="button">
                           <span class="ui-button-icon-primary ui-icon ui-icon-triangle-1-s"></span>
                           <span class="ui-button-text"></span>
                       </a>
                   </span>
               </div>
           </body>
       </html>
   </iframe>
</div>

My java

driver.switchTo().frame(0);
//driver.findElement(By.id("combobox")).clear(); 
List < WebElement > list = driver.findElements(By.tagName("select"));
for (WebElement li: list) {
    System.out.println(li.getText());
}

I am able to print all the data from the dropdown list but that's not my intention. I just want to select a perticular value from that list.How to do this?

david
  • 3,225
  • 9
  • 30
  • 43
Ab123
  • 415
  • 2
  • 13
  • 34
  • duplicate: http://stackoverflow.com/questions/6430462/how-to-select-get-drop-down-option-in-selenium-2 – Henning Luther Aug 18 '16 at 07:09
  • @Henning Luther Did you refer the Html provided because it is totally different – Ab123 Aug 18 '16 at 07:14
  • You have already asked same question http://stackoverflow.com/questions/38990378/how-to-handle-hidden-webelement-in-selenium-webdriver-with-java... – Saurabh Gaur Aug 18 '16 at 07:18
  • yes, this is very similar to pervious one but this time Select sel_1 = new Select(driver.findElement(By.tagName("select"))); sel_1.selectByVisibleText("India"); is not working also i am not getting any error but unable to select the value from dropdown – Ab123 Aug 18 '16 at 07:25
  • In the html, select tag is hidden. Before interacting with it is it visible? And I am guessing India is in the options as it is not present in the html. – Grasshopper Aug 18 '16 at 07:33
  • yes select tag is hidden and let say I have to select Afghanistan – Ab123 Aug 18 '16 at 07:38
  • I also use Xpath to locate the elements but unable to proceed with because it is hidden – Ab123 Aug 18 '16 at 07:39
  • Is it visible on the screen?? – Saurabh Gaur Aug 18 '16 at 07:44
  • Selenium webdriver is meant to mimic the users interactions with a webpage. If you are manually working on this site will you be able to work with a hidden element. Answer is no. You have to figure out what triggers this select to get visible and then interact with it. Otherwise you can use javascript to interact with it if you still wanna go down that route. – Grasshopper Aug 18 '16 at 07:45
  • Yes, it is visible. http://way2automation.com/way2auto_jquery/dropdown.php refer this and then select the tab "Enter Country" – Ab123 Aug 18 '16 at 07:46
  • Are you talking about the registration form with the country option dropbox? – Grasshopper Aug 18 '16 at 07:49
  • @Grasshopper okay you might me asked for credentials. Do one thing just click on sign in and then uname:john and pass: rambo(it's just a test site so sharing with you) – Ab123 Aug 18 '16 at 07:51
  • The element that you need to interact with is not the select element. It is the input element in the span below it, which has the class custom-combobox. The options you need to choose are in the ul tag with class ui-autocomplete ui-front ui-menu ui-widget ui-widget-content and id ui-id-1. Easiest way to figure out the element is to scroll to the element and right click it and select inspect if you in chrome. In the elements window you will see whats happening – Grasshopper Aug 18 '16 at 08:02

2 Answers2

0

write code to click on this span to bring down the dropdown options.

xpath -- "//span[@class='ui-button-icon-primary ui-icon ui-icon-triangle-1-s']"

Wait for the options to display. to select say Algeria and click on it.

xpath -- "//ul[@id='ui-id-1']/li[text()='Algeria']"

enter image description here

Grasshopper
  • 8,908
  • 2
  • 17
  • 32
  • did u check the first Xpath because it is not showing any result in console and when i write the code to click on that element i am getting "no such element " exception – Ab123 Aug 18 '16 at 09:05
  • It is working for me. xpath to the arrow you need to click. Before that I had switched to the 'Enter Country' tab. – Grasshopper Aug 18 '16 at 09:08
  • How it's working for you? I even not able to use the Xpath in console. I am getting no matching node in console and I already switched to Enter Country Tab. – Ab123 Aug 18 '16 at 09:12
  • Added screenshot in answer – Grasshopper Aug 18 '16 at 09:21
0

Please use the below code:

driver.switchTo().frame(driver.findElement(By.cssSelector("iframe.demo-frame"));
// Select class is used to handle drop-downs
Select dropDown = new Select(driver.findElement(By.id("combobox"));
dropDown.selectByVisibleText("<Option you need to select>");

Hope this helps.

k.s. Karthik
  • 731
  • 6
  • 16