Selenium does not find the element

Asked

Viewed 942 times

1

I’m starting to study the Selenium Webdriver and I’m doing some tests.

I created a dummy account on an e-commerce site to automate a product purchase. I can access the page, log in and go to the main screen, but on this page I try to find an element and this is not found.

I always get the error regarding the element not found, but if I inspect the page and use the same xpath, he’s there.

Error:

org.openqa.selenium.TimeoutException: Expected condition failed: waiting for presence of element located by: By.xpath: //div[@class='profile']/a[@href='//www.walmart.com.br/minhaconta'][@class='open-link topbar-buttons icon-topbar-link loaded'] (tried for 10 second(s) with 500 milliseconds interval)
at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:81)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:271)
at br.com.daivid.desafioqa.page.LoginPage.autenticar(LoginPage.java:58)
at br.com.daivid.desafioqa.test.LoginPageTest.test(LoginPageTest.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)

Code:

public class LoginPage {
    private WebDriver driver;
    private WebDriverWait webDriverWait;
    private Actions action;

    public LoginPage(WebDriver driver) {
        this.driver = driver;
        action = new Actions(driver);
        webDriverWait = new WebDriverWait(driver, 10);
        System.setProperty("driver.chromedriver.driver", "BrowserDriver/chromedriver.exe");
    }

    public LoginPage acessarPagina(String url) {
        driver.get(url);
        driver.manage().window().maximize();
        return this;

    }

    public void autenticar(String user, String password) {
        WebElement btEntrar = driver.findElement(By.xpath("//div[@class='login']/div[@class='wrapper']/a[@id='topbar-login-link'][@href='']"));
        btEntrar.click();

        //Esperando o iframe que aparece ficar visível
        webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(By.id("iframeLogin")));

        //alterando frame atual para o frame que aparece na tela e atribuindo a variavel driver
        driver = driver.switchTo().frame(driver.findElement(By.id("iframeLogin")));

        webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.id("signinField")));

        WebElement usuario = driver.findElement(By.xpath("//input[@id='signinField']"));
        usuario.sendKeys(user);

        WebElement senha = driver.findElement(By.xpath("//input[@id='password']"));
        senha.sendKeys(password);

        driver.findElement(By.xpath("//button[@id='signinButtonSend']")).click();

        //Voltando para o frame principal e atribuindo a variavel driver
        driver = driver.switchTo().defaultContent();

        webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(
                "//div[@class='profile']/a[@href='//www.walmart.com.br/minhaconta'][@class='open-link topbar-buttons icon-topbar-link loaded']")));


        WebElement menuUsuario = driver.findElement(By.xpath(
                "//div[@class='profile']/a[@href='//www.walmart.com.br/minhaconta'][@class='open-link topbar-buttons icon-topbar-link loaded']/span[@class='number']/following-sibling::*"));


        action.moveToElement(menuUsuario).build();

        String usuarioLogado = driver.findElement(By.xpath("//strong[text()='Desafio QA']")).getText();

        System.out.println("AUTENTICADO: " + usuarioLogado);
    }

}
No answers

Browser other questions tagged

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