我尝试使用以下网站中的selenium webdriver(java)登录:
www.tokopedia.com。
我成功输入email
和password
,但当代码点击login button
时,会出现一些错误。为什么会发生这种情况以及最佳解决方案是什么?
这是我的代码
`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
}`
这是错误消息
线程中的异常&#34; AWT-EventQueue-0&#34; org.openqa.selenium.StaleElementReferenceException:陈旧的元素引用:元素不再附加到DOM或页面已刷新 有关此错误的文档,请访问:http://seleniumhq.org/exceptions/stale_element_reference.html
答案 0 :(得分:0)
请在找到元素后尝试直接使用sendKeys
。如果找到一个元素并在以后使用它会发生问题。
示例:
WebElement emailLogin = driver.findElement(By.id("inputEmail"));
emailLogin.sendKeys(emailPath);
答案 1 :(得分:0)
可能是您为登录按钮提供的xpath
问题。试试这段代码可以帮助你:
在
private void btnOkActionPerformed(java.awt.event.ActionEvent evt)
用以下代码替换您的代码:
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);
}