java - Selenium WebDriver By.xpath 不能一直工作

标签 java xpath selenium webdriver

信息:

我从配置文件中获取 fieldXpath,它是 "//input[@id='signin_password']"

HTML :

<li><input type="password" name="signin[password]" id="signin_password" /></li>

有效:(但不总是)

捕获...

public void doAction(WebDriver driver) throws TestException {
        try {
             WebElement el = driver.findElement(By.xpath(fieldXpath));
             el.clear();
             el.sendKeys(fieldValue);
         } catch (Exception e) {
            throw new TestException(this.getClass().getSimpleName() + ": problem while doing action : " + toString());
         }
    }

是否有使此代码与 XPath 一起工作的解决方案?

最佳答案

我发现了问题...:selenium WebDriver StaleElementReferenceException

*This may be caused because the page isn't loaded completely when the code starts or changes when the code is executed. You can either try to wait a little longer for the element or catch the StaleReferenceException and try again finding the div and the span.*

我的代码:(在每个字段之前调用这些函数)

/**
 * Handle StaleElementReferenceException
 * @param elementXpath
 * @param timeToWaitInSec
 */
public void staleElementHandleByXpath(String elementXpath, int timeToWaitInSec) {
    int count = 0;
    while (count < 10) {
        try {
            WebElement slipperyElement = driver.findElement(By.xpath(elementXpath));
            if (slipperyElement.isDisplayed()) {
                slipperyElement.click(); // may throw StaleElementReferenceException
            }
            count = count + 10;
         } catch (StaleElementReferenceException e) {
            count = count + 1; // try again
         } catch (ElementNotVisibleException e) {
             count = count + 10; // get out
         } catch (Exception e) {
             count = count + 10; // get out
         } finally {
            // wait X sec before doing the action
            driver.manage().timeouts().implicitlyWait(timeToWaitInSec, TimeUnit.SECONDS);
        }
    }
}

/**
 * Wait till the document is really ready
 * @param js
 * @param timeToWaitInSec
 */
public void waiTillDocumentReadyStateComplete(JavascriptExecutor js, int timeToWaitInSec) {
    Boolean ready = false;
    int count = 0;
    while (!ready && count < 10) {
        ready =  (Boolean) js.executeScript("return document.readyState == 'complete';");
        // wait X sec before doing the action
        driver.manage().timeouts().implicitlyWait(timeToWaitInSec, TimeUnit.SECONDS);
        count = count + 1;
    }
}

关于java - Selenium WebDriver By.xpath 不能一直工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13091442/

相关文章:

c# - 如何遍历 X 和 Y 的所有可能组合

shell - Scrapy:在 shell 中使用xpath检索到的数据,但未在项中检索

python - Xpath - 在 xpath 结果中搜索

python - 如何使(样式 ="display: none;")元素在 python selenium 的 HTML 中可见?

python - 如何从动态网站 python selenium 中检索表

java - jira插件项目中的maven依赖错误

java - 使用相机时在android中创建一个边界框

Java servlet 返回 404 错误

xslt - 将字符串传递到需要节点的模板参数中?

python - ModuleNotFoundError : No module named 'webdriver_manager' error even after installing webdrivermanager