0

Selenium WebDriver statement is skipping after ajax call for example if we use hotmail signup and want to check whether email is available or not I input the text and on lost focus hotmail return a message (email is available) or (email not available) so now after input I want to check the message returned using locator it does not recognize right after input I've to put extra input lines to some other field and than have to return to locate the message following is code snippet

WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
          driver.get(Constant.SIGNUP_URL);
          driver.manage().window().maximize();
          driver.findElement(By.xpath(".//*[@id='MemberName']")).sendKeys("Abigayle.Elsie");
          driver.findElement(By.xpath(".//*[@id='FirstName']")).sendKeys("Anabia");
          driver.findElement(By.xpath(".//*[@id='FirstName']")).sendKeys("Anabia");
          String str = driver.findElement(By.xpath(".//*[@id='CredentialsInputPane']/fieldset/div[2]/div/div[3]")).getText();
          System.out.println(str);
          String stravailbl = "is available.";
            if(driver.findElement(By.xpath(".//*[@id='CredentialsInputPane']/fieldset/div[2]/div/div[3]")).getText().contains(stravailbl))
            {
                System.out.println("email is available");
            }
            else
            {
                System.out.println("email not available");
            }
  • 1
    Taking a wild guess, but you likely need to wait until the AJAX call is completed. You can achieve this by using a WebDriverWait and wait until the text is present and then have a catch that handles it. – Paul Muir Apr 25 '16 at 16:01
  • If it works let me know and I will convert it to an answer. – Paul Muir Apr 26 '16 at 0:37
0

Solution 1 :

I use PageFactory with AjaxElementLocatorFactory - PageFactory is a support class for the Selenium 2 Page Objects pattern which you are using, and the AjaxElementLocatorFactory is the factory for the element locators. In your case the constructor will looks like:

public GoogleResultsPage() { 
    PageFactory.initElements(new AjaxElementLocatorFactory(driver, 15), this);
}

This code will wait maximum of 15 seconds until the elements specified by annotations will appear on the page, in your case the homePageLink which will be located by xpath. You will not need to use ElementPresent class.

AjaxElementLocatorFactory uses SlowLoadableComponent internally.

Solution 2 :

static void waitForPageLoad(WebDriver wdriver) {
    WebDriverWait wait = new WebDriverWait(wdriver, 60);

    Predicate<WebDriver> pageLoaded = new Predicate<WebDriver>() {

        @Override
        public boolean apply(WebDriver input) {
            return ((JavascriptExecutor) input).executeScript("return document.readyState").equals("complete");
        }

    };
    wait.until(pageLoaded);
}

Give a try, Hope it helps!

0

The reason that you're seeing this is because the AJAX event has to make the roundtrip and doesn't immediately appear on the DOM, although you attempt to findElement on it immediately after entering the name.

My favorite method of handling this is to use Selenium's built-in waits with an ExpectedConditions check.

Try adding the following after entering the following after kicking off the AJAX trigger:

WebDriverWait wait = new WebDriverWait(driver, 15);
String availabilityText = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='CredentialsInputPane']/fieldset/div[2]/div/div[3]")).getText();
boolean isAvailable = availabilityText.contains("is available");

This will tell Selenium to continue polling the DOM for the element that displays the availability text for up to 15 seconds. Upon locating it, the element gets stored to our availabilityText variable and the execution will continue and we can evaluate the retrieved text.

See also:
- ExpectedConditions Java API
- WebDriver Advanced Usage

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.