陈旧:元素不再附加到 DOM 或页面已刷新
stale: either the element is no longer attached to the DOM or the page has been refreshed
我正在尝试在以下网站中使用 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
}`
这是错误信息
线程 "AWT-EventQueue-0" org.openqa.selenium.StaleElementReferenceException 中的异常:陈旧的元素引用:元素不再附加到 DOM 或页面已刷新
有关此错误的文档,请访问:http://seleniumhq.org/exceptions/stale_element_reference.html
请尝试找到元素后直接使用sendKeys
。如果您找到一个元素并在很久以后使用它,就会出现问题。
示例:
WebElement emailLogin = driver.findElement(By.id("inputEmail"));
emailLogin.sendKeys(emailPath);
可能是您为登录按钮提供的 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);
}
我正在尝试在以下网站中使用 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
}`
这是错误信息
线程 "AWT-EventQueue-0" org.openqa.selenium.StaleElementReferenceException 中的异常:陈旧的元素引用:元素不再附加到 DOM 或页面已刷新 有关此错误的文档,请访问:http://seleniumhq.org/exceptions/stale_element_reference.html
请尝试找到元素后直接使用sendKeys
。如果您找到一个元素并在很久以后使用它,就会出现问题。
示例:
WebElement emailLogin = driver.findElement(By.id("inputEmail"));
emailLogin.sendKeys(emailPath);
可能是您为登录按钮提供的 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);
}