2

I am using selenium, TestNG and java with eclipse to perform an automated test. I'm having success with commands like click on a button (selenium.click ("button"), pass values ​​to textboxes (selenium.type ("component", "value") and clicks too, but when it comes with a component type dropdown list (relating to common or asp.net MVC) I can not select the field with the command selenium.select ("field", "value").

To select the values ​​and even the fields, I am using XPath to it, but even so, with the dropdown list can not, or can partially.

When a drop-down list accepts the value I type, I can use the selenium.click but if not, nothing I've tried so far works.

5 Answers 5

2

using webdriver you can do it with Select class i have posted a code which was working below please have a look on that,Select Class had api to select the drop down values by its index as well as value,have a look on Select api for more info

   public static void dropdown() 
    {
    WebDriver driver = new FirefoxDriver();
    driver.get("http://demosthenes.info/blog/166/HTML-Forms-Drop-down-Menus");
    Select sele = new Select(driver.findElement(By.id("state")));
    sele.selectByIndex(1);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry this is not gonna work. Select class only works for elements with <select> tags which is not the case for asp.net. I am trying to find the solution for this problem as well, but no luck so far...
@Lucas if possible please post your html dom so that we can also help you to solve the problem
2

There are several ways to select elements from dropdown list. Below are some of them you can keep them as common drop-down operation and call what ever method you need.

//select the dropdown using "select by visible text"
        public static void dropDownSelectByText(WebElement webElement, String VisibleText){
            Select selObj=new Select(webElement);
            selObj.selectByVisibleText(VisibleText);
        }

//select the dropdown using "select by index"
public static void dropDownSelectByIndex(WebElement webElement, int IndexValue){
    Select selObj=new Select(webElement);
    selObj.selectByIndex(IndexValue);
}

//select the dropdown using "select by value"
public static void dropDownSelectByValue(WebElement webElement, String Value){
    Select selObj=new Select(webElement);
    selObj.selectByValue(Value);
}

You can call above methods like

CommonPageOperations.dropDownSelectByValue(selectSubClientFromDropDownXpath,strSubClientName);

Drop down list appear only if mouse move to specific location, Then you have to use Actions as well

public void mouseMoveToExpandIcon(){
        Actions action = new Actions(driver);
        action.moveToElement(expandButtonXpath).perform();
    }

Comments

0
WebElement select = driver.findElement(By.id("selection"));
List<WebElement> options = select.findElements(By.tagName("option"));
for (WebElement option : options) {
  if("Germany".equals(option.getText()))
    option.click();
}

Comments

0
Actions actions = new Actions(driver);
WebElement dBox1= (new    WebDriverWait(driver,10)).until(ExpectedConditions.elementToBeClickable(By.id("selection""))).    selectByVisibleText("");
actions.moveToElement(dBox1);
actions.click();
actions.perform();

Comments

0

You have to use Select in selenium to select from the drop down value.

//by ID

WebDriver driver = new FirefoxDriver();
new Select (driver.findElement(By.id("usState"))).selectByVisibleText("FL");

//by XPath

new Select (driver.findElement(By.xpath("xPath for dropdown"))).selectByVisibleText("FL");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.