【问题标题】:Selenium WebDriver By.xpath doesn't work all the timeSelenium WebDriver By.xpath 始终无法正常工作
【发布时间】:2012-10-26 17:15:07
【问题描述】:

信息:

我从配置文件中得到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 一起工作的解决方案?

【问题讨论】:

  • 你能分享那个元素的 HTML 代码吗?
  • 您也可以指定正在使用的浏览器。旧版本的 IE 对 XPath 的行为不正常。没有区别,但你也可以试试 fieldXpath = //input[contains (@id,"signin_password")]
  • 谢谢大家,但这是另一个问题:selenium WebDriver StaleElementReferenceException
  • 您对这里的答案不满意吗?请接受答案。
  • 标签: java xpath selenium webdriver


    【解决方案1】:

    我发现了问题...: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;
        }
    }
    

    【讨论】:

      【解决方案2】:

      使用单个 ' 引号而不是 "。所以

      String fieldXpath = "//input[@id='signin_password']"; 
      

      【讨论】:

        猜你喜欢
        相关资源
        最近更新 更多
        热门标签