Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FindBy with Select Element

I'm trying to run the following code but i keep getting a null pointer on the Select element ...

Here is the example of my code I use:

  @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 can't use the Driver.findElement function so we have to find a way to work arround with the Find By .. I putted a sout after the dropdown but it just gave me Null.

like image 751
Dimitri Jambers Avatar asked Sep 11 '25 23:09

Dimitri Jambers


2 Answers

You can always use xpath to locate the select element if you don't have className, id or name. you're getting an exception because the constructor for select is expecting a not null WebElement

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

like image 128
ingrid.e Avatar answered Sep 13 '25 13:09

ingrid.e


First of all select class has constructor like :

Select(WebElement element)

So if you do 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 find element is correct.

You can check more about Select mechanism here

like image 27
Helping Hands Avatar answered Sep 13 '25 12:09

Helping Hands