FindBy with select element

I am trying to run the following code, but I keep getting a null pointer on the Select ...

Here is an example of my code that I am using:

  @FindBy(id="ddCompany")
    WebElement Select;

  public void Test(){

    driver.findElement(By.id("igtxtdfUsername")).sendKeys("dimitri");
        Select dropdown = new Select(Select);
        dropdown.getOptions().get(1).click();

    driver.findElement(By.id("igtxtdfPassword")).sendKeys("dimitri");
    driver.findElement(By.id("Login")).click();
    driver.quit();

      

We cannot use the Driver.findElement function, so we need to find a way to work with Find By. I laid out sout after the dropdown, but it just gave me Null.

+3


source to share


4 answers


I fixed the problem today,



The problem is that I needed to use pagefactory, and because I used the Webdriver page object where I define my Webdrivers (all 3 different browsers) I needed to put the Pagefactory right before my function, now it doesn't return a null pointer because that now the function is getting the correct Driver to use and everything is working fine.

0


source


You can always use xpath to find the select element if you don't have a class name, name or name. you get an exception because the constructor for the select expects a non-null WebElement



https://code.google.com/p/selenium/source/browse/java/client/src/org/openqa/selenium/support/ui/Select.java

+2


source


First of all, it has a constructor like:

Select(WebElement element)

      

So, if you do it like below, it should work:

@FindBy(id="ddCompany")
private WebElement Select;
Select dropdown = new Select(Select);
dropdown.getOptions().get(1).click();

      

Make sure your ID for the search item is correct.

You can read more about Select Mechanism here

+1


source


You must find an element for:

  WebElement select;

      

This selection variable is not initialized, so you will get a NullPointerException on the following line:

  Select dropdown = new Select(Select);

      

So findElement for the selected variable, then you only get the dropdown options. Otherwise it will throw an exception.

0


source







All Articles