vendredi 6 octobre 2017

stale: either the element is no longer attached to the DOM or the page has been refreshed

i want to automate login using selenium java in this website www.tokopedia.com . i have success to input email and password but when the code click to login button it gives some error. why it can happen and what should i do?

This is my code

`private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {                                      
    // TODO add your handling code here:
    String filePath = filePathField.getText();
    String emailPath = emailField.getText();
    String passwordPath = passwordField.getText();
    System.setProperty("webdriver.gecko.driver","C:\\Gecko\\geckodriver.exe");
    WebDriver driver  = new FirefoxDriver();
    WebDriverWait wait = new WebDriverWait(driver,20);

    driver.get("http://ift.tt/1ulDDqk");

    driver.findElement(By.xpath("html/body/header/div/div/div[3]/ul/li[2]/button")).click();
    driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@id='iframe-accounts']")));
    WebElement emailLogin = driver.findElement(By.id("inputEmail"));
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    WebElement passwordLogin = driver.findElement(By.id("inputPassword"));
    driver.findElement(By.xpath("//*[contains(text(), 'Masuk ke Tokopedia')]")).isDisplayed();

  wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(text(), 'Masuk ke Tokopedia')]"))).click();
    driver.switchTo().defaultContent();
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("inputEmail")));            wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("inputPassword")));          wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//[contains(text(), 'Masuk ke Tokopedia')]"))).click();
    emailLogin.sendKeys(emailPath);
    passwordLogin.sendKeys(passwordPath);
}                                     

private void emailFieldActionPerformed(java.awt.event.ActionEvent evt) {                                           
    // TODO add your handling code here:
}                                          

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://ift.tt/1cNmMj1 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(() -> {
        new NewJFrame().setVisible(true);
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton btnOk;
private javax.swing.JTextField emailField;
private javax.swing.JTextField filePathField;
private javax.swing.JPasswordField passwordField;
// End of variables declaration                   
}`

This is the error message

`

Exception in thread "AWT-EventQueue-0" org.openqa.selenium.StaleElementReferenceException: The element reference of stale: either the element is no longer attached to the DOM or the page has been refreshed For documentation on this error, please visit: http://ift.tt/1h3Z8lU




Aucun commentaire:

Enregistrer un commentaire