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.
source to share
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.
source to share
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
source to share
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.
source to share