Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting a value from <md-select> tag using Selenium Webdriver

I am using Selenium Webdriver and working on automating an AngularJS Web App on Chrome. It was going pretty well until I hit a dropdown list on the app. My test keeps crashing everytime I try to select a value from it. I have been doing my research on this and I have only seen 2 solutions (both of which I have tried but don't work)

  1. Use the Select object. This doesn't work because the html tag is not <select>, its <md-select> and this fails the test.
  2. I then tried to just click on the dropdown element and click on the value - driver.findElement(By.xpath("xpath to dropdown list")).click(); and driver.findElement(By.xpath("xpath do dropdown value")).click();

With example 2, I also tried creating it as a WebElement variable and calling click() separate, but this did not work either.

Any ideas on how I can solve this issue?

Update

HTML for the dropdown list

<div ng-switch-when="dropdown" class="ng-scope">
    <zf-form-dropdown>
        <div class="dropdown">
            <div layout="row">
                <div flex="50" class="quote-label">
                    <p ng-bind-html="::label" class="ng-binding">Title</p>
                </div>
                <div ng-show="false" flex="10" class="tooltip-icon ng-hide" ng-click="showToolTip(field.get('toolTip'))" role="button" tabindex="0" aria-hidden="true"><img src="img/[email protected]"></div>

                <md-select flex="" ng-disabled="quote.isRated() || !input.enabled" ng-change="onDropdownChange()" ng-model="input.value" class="ng-valid md-default-theme ng-touched ng-dirty" role="combobox" id="select_0Q9" aria-haspopup="true" aria-expanded="false" aria-labelledby="select_label_0I1" tabindex="0" aria-disabled="false" aria-invalid="false" aria-owns="select_menu_0Q8"><md-select-label class="md-select-label md-placeholder" id="select_label_0I1"><span></span><span class="md-select-icon" aria-hidden="true"></span></md-select-label></md-select>
            </div>
        </div>
    </zf-form-dropdown>
</div>

HTML for the value I want to select

<md-option ng-value="::item.val" ng-selected="item.checked" ng-repeat="item in getOpts()" tabindex="0" class="ng-scope" role="option" aria-selected="false" id="select_option_0QD" value="MR">
    <div class="md-text ng-binding">Mr</div>
    <div class="md-ripple-container"></div>
</md-option>

The xpath for the dropdown list is //*[@id="select_0Q9"] The xpath for the dropdown value is //*[@id="select_option_0QD"]

like image 895
Dan Avatar asked Sep 27 '22 12:09

Dan


2 Answers

If you are sure that your Xpath is fine then you can also click using javascriptexecutor so try it out like below:-

WebElement element= driver.findElement(By.xpath("//md-option[@id='select_option_0QD']/div[1]"));

JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);

Please feel free to locate the element in above code as per your convenience .

As per me your xpath of dropdown should be like below:-

//md-option[@id='select_option_0QD']

And xpath of first div which I suppose want to click is:-

//md-option[@id='select_option_0QD']/div[1]

Change [1] to [2] if you want 2nd value.

In an another aspect you can also store all your element in the list(As you know you can't use select) and click them as per your need or all.

For that you need to use xpath like:-

//md-option[@id='select_option_0QD']/div

Now implement it into code:-

List<WebElement> allelemts = driver.findElements(By.xpath("//md-option[@id='select_option_0QD']/div"));
  for(WebElement ele: allelemts){

    driver.findElement(By.xpath("//md-option[@id='select_option_0QD']")).click();

    JavascriptExecutor executor = (JavascriptExecutor) driver;
    executor.executeScript("arguments[0].click();", ele);

  }

Hope it will help you :)

like image 91
Shubham Jain Avatar answered Sep 30 '22 08:09

Shubham Jain


Since you are receiving a NoSuchElementException exception, I believe the issue lies in Selenium not able to identify the element. Try any of the following wait methods and then try to click the element.

Explicit Wait: (Most Preferred)

WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("elementID")));

or

WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("elementID")));

Implicit Wait:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

Sleep: (Try to avoid this unless absolutely necessary)

Thread.sleep(1000);

EDIT: Added code to check for element's presence using findElements method.

Before using any of these wait methods you could also check for the presence of your element using findElements method.

List<WebElement> element = driver.findElements(By.id("elementId"));
    if (element.size() == 0) {
            System.out.println("Element not found");
    } else{
            System.out.println("Element found");
    }
like image 30
justcurious Avatar answered Sep 30 '22 06:09

justcurious