0

I am new to selenium and I am finding difficulty in getting option from dropdown also how to take className if the class has space in between,please help me,thanks in advance.

I used select with selectbyindex also partialvisibletext but nothing worked.

This is the HTML code

<div class="form-group" xpath="1">
  <label>Source of Info</label>      
 <!-- <select class="form-control flat-control-inner" ng-model="userPersonal.sourceOfInfo.id" required> 
  <option ng-value="0">--Select--</option>          
  <option data-ng-repeat="si in sourceOfInfoList" data-ng-value="{{si.id}}" ng-selected="si.id==userPersonal.sourceOfInfo.id">{{si.name}}</option>                                  
  </select> -->
     <select class="form-control flat-control-inner dateonly ng-valid ng-touched ng-empty ng-dirty ng-valid-parse" ng-model="userPersonal.sourceOfInfo.id" ng-options="si.id as si.name for si in sourceOfInfoList" style="">
      <option value="" class="">--Select--</option>
      <option label="Newspaper" value="number:1">Newspaper</option>
      <option label="Facebook" value="number:2" selected="selected">Facebook</option>
      <option label="Twitter" value="number:3">Twitter</option>
      <option label="Television" value="number:4">Television</option>
      <option label="Others" value="number:5">Others</option>
    </select>   
  </div>

This is what I tried

WebElement source_dropdown = driver.findElement(By.xpath("//select[@class='form-control flat-control-inner dateonly ng-valid ng-touched ng-empty ng-dirty ng-valid-parse']"));
Select source = new Select(source_dropdown);
source.selectByIndex(1);

This what I am getting in console

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//select[@class='form-control flat-control-inner dateonly ng-valid ng-touched ng-empty ng-dirty ng-valid-parse']"}
Guy
  • 46,488
  • 10
  • 44
  • 88
Preeti
  • 7
  • 7
  • Add some [wait](https://stackoverflow.com/questions/11736027/webdriver-wait-for-element-using-java) – Guy Apr 03 '19 at 07:43
  • tried with that too using implicitwait – Preeti Apr 03 '19 at 08:32
  • First confirm whether you can find this by your locator manually, if yes then use explicit wait like element to be clickable and then try else try with other locator. – SRM21 Apr 03 '19 at 08:45

3 Answers3

0

Use WebDriverWait to handle dynamic element.If the code not work Please check if there any iframe available and if then you have to switch to iframe first to access the select element.

WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement source_dropdown=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//select[@class='form-control flat-control-inner dateonly ng-valid ng-touched ng-empty ng-dirty ng-valid-parse']")));
Select source = new Select(source_dropdown);
source.selectByIndex(1);
KunduK
  • 32,888
  • 5
  • 17
  • 41
0

Your are unable to find the drop-down itself, try xpath with different attribute like

//select[@ng-model='userPersonal.sourceOfInfo.id']

or

//div[@class='form-group']/following::select
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
sree raja
  • 177
  • 6
0

Try this one to select option with displayed text matching the argument

//xpath = select[@ng-model='userPersonal.sourceOfInfo.id']
WebElement dropDownListBox = driver.findElement(By.xpath("//select[@ng-model='userPersonal.sourceOfInfo.id']"));
Select clickThis = new Select(dropDownListBox);
clickThis.selectByVisibleText(value);//value is an option visible in dropdown
PRERNA PAL
  • 381
  • 2
  • 10
Sandeep K
  • 56
  • 3