Jordan Benyon
Jordan Benyon

Reputation: 320

findElement that is generated HTML during runtime

I'm trying to find an element that is generated from the PCA Predict API, found in this link here. http://www.pcapredict.com/en-gb/address-capture-software/

The code I have at the moment is as follows but it throws an timeout exception due to it not finding any elements. Yet the xpath is correct as I have checked it in developer tools.

    By PCA = By.id("inputPCAnywhere");
    driver.findElement(PCA).clear();
    driver.findElement(PCA).sendKeys(ValidPostcode);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='pcaitem pcafirstitem']")));
    driver.findElement(By.xpath("//div[@class='pcaitem pcafirstitem']")).click();   

The element is visible on the page, and developer tools only returns one result that that xpath, there is no ID's to find it by.

Upvotes: 1

Views: 74

Answers (1)

alecxe
alecxe

Reputation: 473863

It looks like that the first item is getting "selected" by default leading to it's class value being equal to the following:

<div class="pcaitem pcafirstitem pcaselected"...>...</div>

All other following results have only pcaitem class, but none have a pcaitem pcafirstitem class value.

In other words, your problem is the strict class match. I would improve the locator to have a partial match on the class attribute. For instance, with a CSS selector:

wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".pcaitem.pcafirstitem")));

Upvotes: 1

Related Questions