-2

I'm learning automation using Selenium. I'm testing angularjs application. How do I write a java code to select random options from a dropdown

Html:

<select ng-model="attribute" ng-disabled="disabled" ng-required="draftstatus !== 'Incomplete' &amp;&amp; isrequired" ng-show="isrequired" ng-change="change" class="ng-pristine ng-invalid ng-invalid-required ng-touched" required="required" xpath="1" style="">
    <option value="">Please Select</option>
    <!-- ngRepeat: option in collection -->
    <option ng-repeat="option in collection" ng-selected="option === attribute" class="ng-binding ng-scope" value="Apple">Apple</option>
    <!-- end ngRepeat: option in collection -->
    <option ng-repeat="option in collection" ng-selected="option === attribute" class="ng-binding ng-scope" value="Mayo">Mayo</option>
    <!-- end ngRepeat: option in collection -->
    <option ng-repeat="option in collection" ng-selected="option === attribute" class="ng-binding ng-scope" value="Orange">Orange</option>
    <!-- end ngRepeat: option in collection -->
    <option ng-repeat="option in collection" ng-selected="option === attribute" class="ng-binding ng-scope" value="Colorado">Colorado</option>
    <!-- end ngRepeat: option in collection -->
    <option ng-repeat="option in collection" ng-selected="option === attribute" class="ng-binding ng-scope" value="Utah">Utah</option>
    <!-- end ngRepeat: option in collection -->
    <option ng-repeat="option in collection" ng-selected="option === attribute" class="ng-binding ng-scope" value="Mt.Titlis">Mt. Titlis</option>
    <!-- end ngRepeat: option in collection -->
</select>
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

0

you have to treat drop down as normal element. select class will not work here. click on dropdown element then option list will open then click one of the option element of your choice

to open dropdown -

By dropdownLoc  =  By.xpath(".//option[text()='Please Select']/parent::select[@ng-model='attribute']");
WebDriverWait wait = new WebDriverWait(driver, 10);
        dropdown = wait.until(ExpectedConditions.elementToBeClickable(dropdownLoc));
dropdown.click();    

click/select option addition to it you can pass dropdown values into xpath and can make it dynamic -

wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//option[@value='Mayo']")))
Dev
  • 2,739
  • 2
  • 21
  • 34
0

To click on a random option within the dropdown as the element is Angular element you have to induce WebDriverWait for the elementToBeClickable() and you can use the following Locator Strategy:

  • xpath:

    WebElement placeNames = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//select[@class='ng-pristine ng-invalid ng-invalid-required ng-touched' and @ng-model='attribute'][@ng-change='change' and @ng-show='isrequired']")));
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//select[@class='ng-pristine ng-invalid ng-invalid-required ng-touched' and @ng-model='attribute'][@ng-change='change' and @ng-show='isrequired']/option[text()='Please Select']"))).click();
    new WebDriverWait(driver, 20).until(ExpectedConditions.visibility_of_all_elements_located(By.xpath("//select[@class='ng-pristine ng-invalid ng-invalid-required ng-touched' and @ng-model='attribute'][@ng-change='change' and @ng-show='isrequired']//option")));
    Select pNames = new Select(placeNames);
    pNames.selectByVisibleText("Apple");
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352