Selenium Webdriver

Asked

Viewed 1,412 times

1

I am learning to program test automation using Selenium Webdriver with Java, but when I run the same test in Firefox and Internet Explorer the following message appears:

Exception in thread "main" org.openqa.Selenium.Nosuchelementexception: Unable to locate element: #logoff-button For Documentation on this error, Please visit: http://seleniumhq.org/exceptions/no_such_element.html

I’d like to know the reason for the mistake?

Can someone help me?

 package br.com.painelmiaedes.ecovec.menu;

import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Menupainel {

    private static WebDriver driver = null;

    public void Menu(){

        Scanner entrada = new Scanner(System.in);
        int opcaoteste = 0;
        int opcaobrowser = 0;


    do{

        System.out.println("**************************************************************************************");
        System.out.println("********************     SEJA BEM VINDO AO PAINEL MI TESTE     ***********************");
        System.out.println("********************     ********************************      ***********************");
        System.out.println("********************     ESCOLHA A OPÇÃO PARA O SEU TESTE      ***********************");
        System.out.println("**************************************************************************************");
        System.out.println("********************     OPÇÕES DE TESTE NO LOGIN              ***********************");
        System.out.println("********************     1 - LOGIN ESTADO ESPIRITO SANTO       ***********************");
        System.out.println("********************     2 - LOGIN REGIONAL NORTE              ***********************");
        System.out.println("********************     3 - LOGIN REGIONAL SUL                ***********************");
        System.out.println("********************     4 - LOGIN REGIONAL CENTRO             ***********************");
        System.out.println("********************     5 - LOGIN REGIONAL METROPOLITANA      ***********************");
        System.out.println("**************************************************************************************");
        System.out.println("********************     OPÇÕES DE BROWSER                     ***********************");
        System.out.println("********************     1 - GOOGLE CHROME                     ***********************");
        System.out.println("********************     2 - MOZILLA FIREFOX                   ***********************");
        System.out.println("********************     3 - INTERNET EXPLORER                 ***********************");
        System.out.println("**************************************************************************************");

        Scanner entradateste = new Scanner(System.in);
        Scanner entradabrowser = new Scanner(System.in);

        System.out.println("\n\n Digite a opção de login:");
        opcaoteste = entradateste.nextInt();

        if(opcaoteste == 0){
            System.out.printf("Você escolheu a opcao sair: " + "\n" + "\n" + opcaoteste + " - " + "saindo......" + "\n");   
        }else if(opcaoteste == 1){
            System.out.printf("Você escolheu a opcao de teste de login: " + "\n" + "\n" + opcaoteste + " - " + "Espirito Santo" + "\n");
        }else if(opcaoteste == 2){
            System.out.printf("Você escolheu a opcao de teste de login: " + "\n" + "\n" + opcaoteste + " - " + "Regional Norte" + "\n");
        }else if(opcaoteste == 3){  
            System.out.printf("Você escolheu a opcao de teste de login: " + "\n" + "\n" + opcaoteste + " - " + "Regional Sul" + "\n");
        }else if(opcaoteste == 4){
            System.out.printf("Você escolheu a opcao de teste de login: " + "\n" + "\n" + opcaoteste + " - " + "Regional Centro" + "\n");
        }else if(opcaoteste == 5){
            System.out.printf("Você escolheu a opcao de teste de login: " + "\n" + "\n" + opcaoteste + " - " + "Regional Metropolitana" + "\n");
        }

        switch(opcaoteste){
        case 1: 
            System.out.println("\nDigite o browser para a execucao do teste:");
            opcaobrowser = entradabrowser.nextInt();

            if(opcaobrowser == 1){
                System.out.printf("Você escolheu o Browser:" + opcaobrowser + "-" + "Google Chrome" + "\n");

                System.setProperty("webdriver.chrome.driver","drivers/chromedriver.exe");
                driver = new ChromeDriver();
                driver.get("http://painel.miaedes.com.br");
                driver.manage().window().maximize();
                driver.findElement(By.id("username")).click();
                driver.findElement(By.id("username")).sendKeys("");
                driver.findElement(By.id("password")).click();
                driver.findElement(By.id("password")).sendKeys("");
                driver.findElement(By.id("login-button")).click();
                driver.findElement(By.id("logoff-button")).click();

            }else if(opcaobrowser == 2){

                System.out.printf("Você escolheu o Browser:" + opcaobrowser + "-" + "Mozilla Firefox" + "\n");

                System.setProperty("webdriver.gecko.driver","drivers/geckodriver.exe");
                driver = new FirefoxDriver();
                driver.get("http://painel.miaedes.com.br");
                driver.manage().window().maximize();
                driver.findElement(By.id("username")).click();
                driver.findElement(By.id("username")).sendKeys("");
                driver.findElement(By.id("password")).click();
                driver.findElement(By.id("password")).sendKeys("");
                driver.findElement(By.id("login-button")).click();
                driver.findElement(By.id("logoff-button")).click();
            }else if(opcaobrowser == 3){

                System.out.printf("Você escolheu o Browser:" + opcaobrowser + "-" + "Internet Explorer" + "\n");

                System.setProperty("webdriver.ie.driver","drivers/IEDriverServer.exe");
                driver = new InternetExplorerDriver();
                driver.get("http://painel.miaedes.com.br");
                driver.manage().window().maximize();
                driver.findElement(By.id("username")).click();
                driver.findElement(By.id("username")).sendKeys("");
                driver.findElement(By.id("password")).click();
                driver.findElement(By.id("password")).sendKeys("");
                driver.findElement(By.id("login-button")).click();
                driver.findElement(By.id("logoff-button")).click();         
            }           
            break;

        case 2: 
            System.out.println("\nDigite o browser para a execucao do teste:");
            opcaobrowser = entradabrowser.nextInt();

            if(opcaobrowser == 1){
                System.out.printf("Você escolheu o Browser:" + opcaobrowser + "-" + "Google Chrome" + "\n");

                System.setProperty("webdriver.chrome.driver","drivers/chromedriver.exe");
                driver = new ChromeDriver();
                driver.get("http://painel.miaedes.com.br");
                driver.manage().window().maximize();
                driver.findElement(By.id("username")).click();
                driver.findElement(By.id("username")).sendKeys("");
                driver.findElement(By.id("password")).click();
                driver.findElement(By.id("password")).sendKeys("");
                driver.findElement(By.id("login-button")).click();
                driver.findElement(By.id("logoff-button")).click();

            }else if(opcaobrowser == 2){

                System.out.printf("Você escolheu o Browser:" + opcaobrowser + "-" + "Mozilla Firefox" + "\n");
                System.setProperty("webdriver.gecko.driver","drivers/geckodriver.exe");
                driver = new FirefoxDriver();
                driver.get("http://painel.miaedes.com.br");
                driver.manage().window().maximize();
                driver.findElement(By.id("username")).click();
                driver.findElement(By.id("username")).sendKeys("");
                driver.findElement(By.id("password")).click();
                driver.findElement(By.id("password")).sendKeys("");
                driver.findElement(By.id("login-button")).click();
                driver.findElement(By.id("logoff-button")).click();
            }else if(opcaobrowser == 3){

                System.out.printf("Você escolheu o Browser:" + opcaobrowser + "-" + "Internet Explorer" + "\n");

                System.setProperty("webdriver.ie.driver","drivers/IEDriverServer.exe");
                driver = new InternetExplorerDriver();
                driver.get("http://painel.miaedes.com.br");
                driver.manage().window().maximize();
                driver.findElement(By.id("username")).click();
                driver.findElement(By.id("username")).sendKeys("");
                driver.findElement(By.id("password")).click();
                driver.findElement(By.id("password")).sendKeys("");
                driver.findElement(By.id("login-button")).click();
                driver.findElement(By.id("logoff-button")).click();
              }
            break;

        case 3: 
            System.out.println("\nDigite o browser para a execucao do teste:");
            opcaobrowser = entradabrowser.nextInt();

            if(opcaobrowser == 1){
                System.out.printf("Você escolheu o Browser:" + opcaobrowser + "-" + "Google Chrome" + "\n");

                System.setProperty("webdriver.chrome.driver","drivers/chromedriver.exe");
                driver = new ChromeDriver();
                driver.get("http://painel.miaedes.com.br");
                driver.manage().window().maximize();
                driver.findElement(By.id("username")).click();
                driver.findElement(By.id("username")).sendKeys("");
                driver.findElement(By.id("password")).click();
                driver.findElement(By.id("password")).sendKeys("");
                driver.findElement(By.id("login-button")).click();
                driver.findElement(By.id("logoff-button")).click();

            }else if(opcaobrowser == 2){

                System.out.printf("Você escolheu o Browser:" + opcaobrowser + "-" + "Mozilla Firefox" + "\n");

                System.setProperty("webdriver.gecko.driver","drivers/geckodriver.exe");
                driver = new FirefoxDriver();
                driver.get("http://painel.miaedes.com.br");
                driver.manage().window().maximize();
                driver.findElement(By.id("username")).click();
                driver.findElement(By.id("username")).sendKeys("");
                driver.findElement(By.id("password")).click();
                driver.findElement(By.id("password")).sendKeys("");
                driver.findElement(By.id("login-button")).click();
                driver.findElement(By.id("logoff-button")).click();
            }else if(opcaobrowser == 3){

                System.out.printf("Você escolheu o Browser:" + opcaobrowser + "-" + "Internet Explorer" + "\n");

                System.setProperty("webdriver.ie.driver","drivers/IEDriverServer.exe");
                driver = new InternetExplorerDriver();
                driver.get("http://painel.miaedes.com.br");
                driver.manage().window().maximize();
                driver.findElement(By.id("username")).click();
                driver.findElement(By.id("username")).sendKeys("");
                driver.findElement(By.id("password")).click();
                driver.findElement(By.id("password")).sendKeys("");
                driver.findElement(By.id("login-button")).click();
                driver.findElement(By.id("logoff-button")).click();
              }
            break;

        case 4:
            System.out.println("\nDigite o browser para a execucao do teste:");
            opcaobrowser = entradabrowser.nextInt();

            if(opcaobrowser == 1){
                System.out.printf("você escolheu o Browser:" + opcaobrowser + "-" + "Google Chrome" + "\n");

                System.setProperty("webdriver.chrome.driver","drivers/chromedriver.exe");
                driver = new ChromeDriver();
                driver.get("http://painel.miaedes.com.br");
                driver.manage().window().maximize();
                driver.findElement(By.id("username")).click();
                driver.findElement(By.id("username")).sendKeys("");
                driver.findElement(By.id("password")).click();
                driver.findElement(By.id("password")).sendKeys("");
                driver.findElement(By.id("login-button")).click();
                driver.findElement(By.id("logoff-button")).click();

            }else if(opcaobrowser == 2){

                System.out.printf("Você escolheu o Browser:" + opcaobrowser + "-" + "Mozilla Firefox" + "\n");

                System.setProperty("webdriver.gecko.driver","drivers/geckodriver.exe");
                driver = new FirefoxDriver();
                driver.get("http://painel.miaedes.com.br");
                driver.manage().window().maximize();
                driver.findElement(By.id("username")).click();
                driver.findElement(By.id("username")).sendKeys("");
                driver.findElement(By.id("password")).click();
                driver.findElement(By.id("password")).sendKeys("");
                driver.findElement(By.id("login-button")).click();
                driver.findElement(By.id("logoff-button")).click();
            }else if(opcaobrowser == 3){

                System.out.printf("Você escolheu o Browser:" + opcaobrowser + "-" + "Internet Explorer" + "\n");

                System.setProperty("webdriver.ie.driver","drivers/IEDriverServer.exe");
                driver = new InternetExplorerDriver();
                driver.get("http://painel.miaedes.com.br");
                driver.manage().window().maximize();
                driver.findElement(By.id("username")).click();
                driver.findElement(By.id("username")).sendKeys("");
                driver.findElement(By.id("password")).click();
                driver.findElement(By.id("password")).sendKeys("");
                driver.findElement(By.id("login-button")).click();
                driver.findElement(By.id("logoff-button")).click();
              }
            break;

        case 5:
            System.out.println("\nDigite o browser para a execucao do teste:");
            opcaobrowser = entradabrowser.nextInt();

            if(opcaobrowser == 1){
                System.out.printf("Você escolheu o Browser:" + opcaobrowser + "-" + "Google Chrome" + "\n");

                System.setProperty("webdriver.chrome.driver","drivers/chromedriver.exe");
                driver = new ChromeDriver();
                driver.get("http://painel.miaedes.com.br");
                driver.manage().window().maximize();
                driver.findElement(By.id("username")).click();
                driver.findElement(By.id("username")).sendKeys("");
                driver.findElement(By.id("password")).click();
                driver.findElement(By.id("password")).sendKeys("");
                driver.findElement(By.id("login-button")).click();
                driver.findElement(By.id("logoff-button")).click();

            }else if(opcaobrowser == 2){

                System.out.printf("Você escolheu o Browser:" + opcaobrowser + "-" + "Mozilla Firefox" + "\n");

                System.setProperty("webdriver.gecko.driver","drivers/geckodriver.exe");
                driver = new FirefoxDriver();
                driver.get("http://painel.miaedes.com.br");
                driver.manage().window().maximize();
                driver.findElement(By.id("username")).click();
                driver.findElement(By.id("username")).sendKeys("");
                driver.findElement(By.id("password")).click();
                driver.findElement(By.id("password")).sendKeys("");
                driver.findElement(By.id("login-button")).click();
                driver.findElement(By.id("logoff-button")).click();
            }else if(opcaobrowser == 3){

                System.out.printf("Você escolheu o Browser:" + opcaobrowser + "-" + "Internet Explorer" + "\n");

                System.setProperty("webdriver.ie.driver","drivers/IEDriverServer.exe");
                driver = new InternetExplorerDriver();
                driver.get("http://painel.miaedes.com.br");
                driver.manage().window().maximize();
                driver.findElement(By.id("username")).click();
                driver.findElement(By.id("username")).sendKeys("");
                driver.findElement(By.id("password")).click();
                driver.findElement(By.id("password")).sendKeys("");
                driver.findElement(By.id("login-button")).click();
                driver.findElement(By.id("logoff-button")).click();
              }
            break;

            default:    
                System.out.printf("Opção Inválida digite uma outra opção: \n\n");
          }
       }while(opcaoteste !=0);
    }
}
  • Hello Mom, the problem is on your page. make sure http://painel.miaedes.com.br has a button with id log-off. Try repeating your code less as well. It’s very painful to understand the flow of the test.

2 answers

0

This is a Timeout problem, as described by the colleague. Another solution would be to Implicit Wait.

Example

  1. First it is necessary to make the following import:

    import java.util.concurrent.TimeUnit;

  2. In the section where you want the algorithm to wait, write the following line of code:

    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); // Este método recebe o tempo desejado e unidade de tempo.

This way the algorithm waits for a specific time and not for elements. Can be useful in tests that need to check whether certain elements are loaded at a specific time.

0

This problem occurs because Selenium is trying to perform an action in a location that is not yet loaded. In order for you to be able to do this test you will need Selenium to wait for the browser to load to be able to do the action.

Click example:

Webdriver wait = new WebdDriverWait(driver,10) // este objeto recebe seu driver e o tempo que deve esperar 


wait.until(ExpectedConditions.ElementClickable(By.id("id do botao"))); //nesta linha ele vai aguardar que seja carregado na tela um botão com o id informado
driver.findElement(By.id("id do botao")).click;//esta linha ele irá clicar no botão após a espera

Another way to solve the problem is by using thred but this is not a good practice.

Example:

new.thread.sleep(1000) // aqui ele vai aguar dar 1s antes de clicar 
driver.findElement(By.id("id do botao")).click;//esta linha ele irá clicar no botão após a espera

Browser other questions tagged

You are not signed in. Login or sign up in order to post.