0

I'm trying to login using selenium webdriver (java) in the following website: www.tokopedia.com. I succeed to input email and password but when the code clicks on login button, it gives some error. Why is this happening and what should be best solution?

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("https://www.tokopedia.com");

    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://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    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://seleniumhq.org/exceptions/stale_element_reference.html

Sanchit
  • 315
  • 2
  • 20
brianesa
  • 3
  • 2
  • Possible duplicate of [StaleElementReference Exception in PageFactory](https://stackoverflow.com/questions/44838538/staleelementreference-exception-in-pagefactory) – undetected Selenium Oct 06 '17 at 09:47

2 Answers2

0

Please try to use sendKeys directly after finding the element. There will occur problems if you find an element and work with it much later.

Example:

WebElement emailLogin = driver.findElement(By.id("inputEmail"));
emailLogin.sendKeys(emailPath);
Benjamin Schüller
  • 2,104
  • 1
  • 17
  • 29
0

Probably it's the issue with xpath you provided for login button. Try this code it helps you:

After

private void btnOkActionPerformed(java.awt.event.ActionEvent evt)

Replace your code with this:

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("https://www.tokopedia.com");

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(".//*[@id='global_login_btn']")).isDisplayed();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='global_login_btn']"))).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);
}
Sanchit
  • 315
  • 2
  • 20