I'm having trouble with my Explicit Wait not always working. They will randomly fail, and I'm not sure why. I'm hitting element not found, or Stale Element. What can I do to make them more reliable?
public class FrameRegisterAccount extends BasicTests
{
// Generates Email
String userName = ""+(int)(Math.random()*Integer.MAX_VALUE);
String emailID = "Otto"+userName+"@gmail.com";
@Test (priority=1)
public void NewAccount() throws Exception {
WebDriverWait wait = new WebDriverWait(driver, 15, 2500L);
WebElement signButton = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@class='button-sign']")));
signButton.click();
log.info("New Account Creation Screen Opened");
// Fill out Form
WebElement nameField = wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("first_name")));
nameField.sendKeys("Otto");
driver.findElement(By.name("last_name")).sendKeys("Mation Tester");
driver.findElement(By.name("phone_number")).sendKeys("555-555-3231");
driver.findElement(By.name("listings")).sendKeys("4");
// Enter Email and Password
driver.findElement(By.xpath("//input[@name='email']")).clear();
driver.findElement(By.xpath("//input[@name='email']")).sendKeys(emailID);
driver.findElement(By.xpath("//input[@name='password']")).clear();
driver.findElement(By.xpath("//input[@name='password']")).sendKeys("password");
// Click buttons
driver.findElement(By.xpath("//div[@class='input input-term']//div[@class='checkbox-icon']")).click();
driver.findElement(By.xpath("//span[contains(text(),'Sign Up')]")).click();
Thread.sleep(3000);
log.info("New Account Created");
}
@Test (priority=2)
public void ChangePassword() {
// Wait for Dashboard Button
WebDriverWait wait = new WebDriverWait(driver, 15, 2500L);
WebElement dashButton = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(@class,'success-dashboard')]")));
dashButton.click();
log.info("Go to Dashboard Button Pressed");
// Change Password
WebElement proNameX = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='sidebar-top-item']")));
proNameX.click();
WebElement changePass = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='nf-change-password-btn']")));
changePass.click();
//driver.findElement(By.xpath("//div[@id='nf-change-password-btn']")).click();
driver.findElement(By.name("old_password")).sendKeys("password");
driver.findElement(By.name("new_password")).sendKeys("wordpass");
driver.findElement(By.name("new_password2")).sendKeys("wordpass");
driver.findElement(By.xpath("//button[@type='button']")).click();
log.info("Password Change Completed");
}
Stacktrace:
org.openqa.selenium.StaleElementReferenceException:
stale element reference: element is not attached to the page document
(Session info: headless chrome=72.0.3626.109)
(Driver info: chromedriver=2.46.628388 (4a34a70827ac54148e092aafb70504c4ea7ae926),platform=Linux 4.14.88-88.76.amzn2.x86_64 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'ip-172-31-36-12.us-west-2.compute.internal', ip: '172.31.36.12', os.name: 'Linux', os.arch: 'amd64', os.version: '4.14.88-88.76.amzn2.x86_64', java.version: '1.8.0_131'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.46.628388 (4a34a70827ac54..., userDataDir: /tmp/.org.chromium.Chromium...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:42199}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: LINUX, platformName: LINUX, proxy: Proxy(), rotatable: false, setWindowRect: true, strictFileInteractability: false, takesHeapSnapshot: true, takesScreenshot: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unexpectedAlertBehaviour: ignore, unhandledPromptBehavior: ignore, version: 72.0.3626.109, webStorageEnabled: true}
Session ID: 1a448c188bc68362fa3dc7858ee770b3
at registerAuto.FrameRegisterAccount.ChangePassword(FrameRegisterAccount.java:57)
Stack 2:
org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: //div[@id='sidebar-top-item'] (tried for 15 second(s) with 2500 milliseconds interval) Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03' System info: host: 'ip-172-31-36-12.us-west-2.compute.internal', ip: '172.31.36.12', os.name: 'Linux', os.arch: 'amd64', os.version: '4.14.88-88.76.amzn2.x86_64', java.version: '1.8.0_131' Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.46.628388 (4a34a70827ac54..., userDataDir: /tmp/.org.chromium.Chromium...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:42977}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: LINUX, platformName: LINUX, proxy: Proxy(), rotatable: false, setWindowRect: true, strictFileInteractability: false, takesHeapSnapshot: true, takesScreenshot: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unexpectedAlertBehaviour: ignore, unhandledPromptBehavior: ignore, version: 72.0.3626.109, webStorageEnabled: true} Session ID: 7b255ab6a2cf5b58cf3919904e703b2a at allStagingAutomation.FrameRegisterAccountStaging.ChangePassword(FrameRegisterAccountStaging.java:55)