3

情報 :

fieldXpathは設定ファイルから取得しますが、それは "//input[@id='signin_password']"

HTML :

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

WORKS : (ただし、常にではありません)

キャッチに入る...

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 で動作させるソリューションはありますか?

4

2 に答える 2

2

私は問題を見つけました...: 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;
    }
}
于 2012-10-30T16:38:19.293 に答える
1

'の代わりに一 重引用符を使用してください"。それで

String fieldXpath = "//input[@id='signin_password']"; 
于 2012-10-26T17:24:01.077 に答える