0

I have this code where I fill 2 fields and then click on login button:

driver.findElement(By.xpath(numberInputXPath)).sendKeys(number);
driver.findElement(By.xpath(passwordInputXPath)).sendKeys(password);
driver.findElement(By.xpath(loginButtonXPath)).click();

My problem is that sometimes the login button is pressed with fields incompletely filled. The pattern on incompleteness seems random to me. I have 11 numbers at string number and my password is 8-alphanumeric-char long. Sometimes the fields are completely filled, sometimes the first one is incomplete by half and password is entirely filled and vice-versa. Sometimes they are completed normally and my login is successfully done. Besides thread sleeping and this solution, how can I wait only until my fields are filled?

EDIT

I didn't have time to try the answers' solutions and I will ASAP, but I'm trying to automate login at this page. I wait until Selenium recognizes up right Login button, click on it and then fill the fields as wrote above. I notice that every time sendKeys is executed, the page still has the loading icon on Chrome tab. Always. So I assumed that some JS is always loading at the time of writing.

rado
  • 5,720
  • 5
  • 29
  • 51
  • Let me guess... you're using IE for your browser? – Bill Hileman May 02 '18 at 00:10
  • No, I'm using `ChromeDriver` as browser. I was thinking is some JS could change XPath at the moment Selenium was inserting data on it – rado May 02 '18 at 00:28
  • @GabrielRado, Please check the two text fields for number and password would do validation checking while you filling manually, If so, I guess the validation Javascript function added by developer breaks the selenium normal sendkeys. – yong May 02 '18 at 02:11
  • @GabrielRado There can be a lot of possibilities for the behavior you are observing. Update the question with the relevant _HTML_ – undetected Selenium May 02 '18 at 08:53

2 Answers2

1

I don't have access to all my code at the moment. If you think it might be an ajax issue I do have code to wait for ajax to finish. I'll post that if my available code (below) does not help. It checks the document state for ready, typically set when a page is finished loading, but does not account for things like ajax elements. Worth a try, though, so let me know.

/**
 * Wait for the web page to finish loading
 * @author Bill Hileman
 */
public void waitForPageToLoad() {

    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(new ExpectedCondition<Boolean>() {

        public Boolean apply(WebDriver wdriver) {
            return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");
        }
    });
}
Bill Hileman
  • 2,798
  • 2
  • 17
  • 24
1

I dont know the technology stack of the site you're testing but. There might be a Ajax or Angular issue. I had the same problem where there are many ajax cals when an object is selected. So we had implement a waiter strategy before each Selenium Command.

You can take a look at the article: https://www.swtestacademy.com/selenium-wait-javascript-angular-ajax/ Those waiting strategies must help you overcome those issue.