2

The dropdown list is triggered by an input field. Once the dropdown list is shown, I wanted to select the item based on the index. When I am using PageFactory, (@FindBy) to identify the WebElement, there is always the NullPointerException error.

My question is, is there a (not complicated) way for me to use Select in PageFactory?

  • 1
    i feel this is not a dropdown , but suggested list using ajax call for input box, can you share your code, screenshot of input box and html code? – Keshav Jan 5 '17 at 4:57
  • Try sqa.stackexchange.com/questions/21950/… I guess - you need to replace element by driver.pagefactoryWebElement – NarendraC Jan 5 '17 at 5:38
2

Add a simple wrapper accessor around the WebElement accessor, like this:

@FindBy(id="dropdown")
WebElement dropdownList;

public Select getSelectOptions() {
  return new Select(dropdownList);
}

You can add helpful getter and setter methods too, like

public void setOption(int value)
{
  getSelectOptions().selectByIndex(value);
}

public String getSelectedOption()
{
    return getSelectOptions().getFirstSelectedOption().getText();
}

Using the following way we can use Select in Page Factory.

1

In order to handle items inside a dropdown , Your drop down object should be looks like this. It should be a IList<IWebElement> type

[FindsBy(How = How.ClassName, Using = "<XXXX>")]
 public IList<IWebElement> DropDown { get; set; }

After that using a for each loop go thru each and every item at the DropDown list.

0

Simply use the Select class of Selenium Webdriver.

Select select = new Select(driver);
select.selectByIndex(<index-of-element>);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.