简体   繁体   English

找不到xPath SELENIUM JAVA

[英]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 . 我遇到无法找到xpath的keep throw错误,因为我设置了打开此xpath的循环以插入数据。 Although i set the time wait for existance to 60secound but it still couldn't find it. 尽管我将等待存在的时间设置为60secound,但仍然找不到它。 I been trying alot method calling this by using title or status hence it still not working . 我一直在尝试使用标题或状态来调用此方法,因此它仍然无法正常工作。 Kindly advise 好心提醒

HTML : 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 -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 谢谢

If it's an internal company webpage could I suggest that you give the an 'id' to make your life easier. 如果这是公司内部网站,我建议您提供一个“ id”以简化您的生活。 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. 当您可以使用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: 显式的方法如下,其中10秒是您的秒数:

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 http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM