Damon Ng
Damon Ng

Reputation: 129

Unable to find xPath SELENIUM JAVA

I have issue keep throw error of unable to find the xpath , as i set the loop for opening this xpath to insert data . Although i set the time wait for existance to 60secound but it still couldn't find it. I been trying alot method calling this by using title or status hence it still not working . Kindly advise

HTML :

<a href="javascript: void edit('edit_total_amt')" title="Override total tax amount" onmouseover="status='Override total tax amount'; return true">91.14</a>

CODE :

public void clickOnItemTax () {

        By xPath = By.xpath("//a[contains(@href,'edit_total_amt')]");

        this.sleep(3);
        if (this.waitForExistence(xPath,60))    {
            WebElement domLink = linkGet(xPath); 
            domLink.click();

        } else  {
            JLog.fail("Unable to find a writable item taxdialog!");
        }

    } 

-waitforExistence

public boolean waitForExistence(By by, int timeoutSeconds){
        boolean exists = false;
        Long polling_interval = (long) 250;
        Long timeout = (long) timeoutSeconds * 1000; // in seconds
        Long elapsed = (long) 0;
        while (elapsed <= (timeout)) {
            if (exists(by)) {
                exists = true;
                break;
            }
            try {
                Thread.sleep(polling_interval);
            } catch (InterruptedException e) {
                JLog.warning(JLog.getStackTraceAsString(e));
                break;
            }
            elapsed += polling_interval;
        }
        if (elapsed >= timeout) {
            JLog.warning("waitForExistence waited for " + timeout/1000 + " seconds, but unable to find: " + by.toString());
        }   
        return exists;
    }

Thanks you

Upvotes: 1

Views: 186

Answers (1)

Fi Horan
Fi Horan

Reputation: 512

If it's an internal company webpage could I suggest that you give the an 'id' to make your life easier. If not you can do this. I'm always surprised by people writing their own wait method when you could use either the implicit or explicit wait time in Selenium.

The former is as follows, the only thing to be aware using this method is that when looking for an element it will always wait this long. It is however a much safer way to write your scripts looking for elements and doesn't bloat your code:

driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);

if (driver.findElements(By.cssSelector("//*[@title=\"Override total tax amount\"]")).size()!=0)
{
    driver.findElement(By.cssSelector("//*[@title=\"Override total tax amount\"]")).click();
}
else
{
     JLog.fail("Unable to find a writable item taxdialog!");
}

The explicit way to do it would be as follows where 10 is your seconds:

WebElement myDynamicElement = (new WebDriverWait(driver, 10))
 .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("//*[@title=\"Override total tax amount\"]")));

See the following link for more on this. http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp

Upvotes: 2

Related Questions