• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

413
Views
Difference between using @FindBy and By; what could be thre reason for this error?

I am automating https://www.zoho.com/login.html. This page has sign in section inside a frame. When i switch through different frames and search for my Email/Phone field (Sign In section), i can find it and then hence use the working block (below) but cannot use the similar code (also below) the only difference between two block is how i have identified my userNameInputBox element.

//This is working

       List<WebElement> iframeElements =   driver.findElements(By.tagName("iframe"));
                int nFrames = iframeElements.size();

        //Switching frame to go to login frame  
        for (int i=0; i<=nFrames ; i++) {
            System.out.println("nFrames: " +nFrames);
            driver.switchTo().frame(i);
            if (driver.findElement(By.name("lid")).isDisplayed()){


                driver.findElement(By.name("lid")).sendKeys("myuserName1");
}}

However if i use this (below code) it does not work and instead gives me NullPointerException in the line where i .click my userNameInput into username field.

///This is not working

//Elements

    @FindBy(name="lid")
    WebElement userNameInput;

//Switching frame to go to login frame  
        for (int i=0; i<=nFrames ; i++) {
            System.out.println("nFrames: " +nFrames);
            driver.switchTo().frame(i);
            if (driver.findElement(By.name("lid")).isDisplayed()){
                userNameInput.click(); //clicking the inputBox
                userNameInput.sendKeys("myuserName"); //Sendingkeys into
}}

Can somebody please explain what could be the reason of such anomaly? Or point out what have i missed? I want to use @FindBy for the WebElement.

about 3 years ago · Santiago Trujillo
1 answers
Answer question

0

The @FindBy tag is designed to work with the Page Object model. To use it, you have to initialize the page object with the PageFactory so that the fields are initialized and then "found" by the webdriver.

See https://github.com/SeleniumHQ/selenium/wiki/PageFactory for the documentation on it.

So before you try to use the userNameInput field, you'll have to have some sort of line like

PageFactory.initElements(driver, page); (where the page var is a class using the @FindBy annotation.

about 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error