Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

472
Visualizações
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.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda