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