Selenium WebDriver

1

I'm 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: link

Would you like to know the reason for the error?

Can anyone 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);
    }
}
    
asked by anonymous 04.06.2017 / 01:21

2 answers

0

This problem occurs because the selenium eta tries to perform an action in a location that is not yet loaded. For you to do this test you will need the selenium to wait for the browser to load in order 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
    
17.07.2017 / 06:30
0

This is a Timeout problem, as described by your colleague. Another workaround would be Implicit Waiting .

Example

  • First you need to do the following import:

    import java.util.concurrent.TimeUnit;

  • 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.

  • In this way the algorithm waits for a specific time and not for elements. It can be useful in tests that need to check if certain elements are loaded at a specific time.

        
    17.01.2018 / 12:07