5

セレンの webdriverwait メソッドを特定の IWebElement に適用して、この IWebElement の子要素が利用可能になったときにそれらを取得しています。これは私のコードです...

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IList<IWebElement> elementsA = wait.Until<IList<IWebElement>>((d) =>
{
     return driver.FindElements(By.XPath("//table[@class='boxVerde']"));
});

foreach (IWebElement iwe in elementsA)
{
      wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
      IList<IWebElement> elementsB = wait.Until<IList<IWebElement>>((d) =>
      {
            return iwe.FindElements(By.XPath("tr/td/div/a"));
            //trying to fetch the anchor tags from the element
       });
}

「要素がDOMにアタッチされていません」というエラーが表示され続けます...Webドライバーの待機が機能していないと思います。私は何か間違ったことをしていますか?よろしくお願いします

4

4 に答える 4

0

ポーリング時間が経過するまで例外を無視するようにWebdriverの待機を定義する必要があります。次の Java メソッドは、エレメント util を 60 秒間ポーリングし、ロードされている場合は戻ります。ロードされていない場合はタイムアウト例外をスローします。必要に応じてこれをあなたの言語に変換してください。

public WebElement fluentWait(final By locator, WebDriver driver) {
    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
    .withTimeout(60, TimeUnit.SECONDS)
    .pollingEvery(1, TimeUnit.SECONDS)
    .ignoring(StaleElementReferenceException.class)
    .ignoring(NoSuchElementException.class);
    WebElement foo = wait.until(
    new Function<WebDriver, WebElement>() {
    public WebElement apply(WebDriver driver) {
    return driver.findElement(locator);
    }
         }  );
    return foo;}

それに加えて、単一のアンカー タグ要素を探している場合は、xpath を次のように使用します。

       iwe.FindElements(By.XPath(" //a[text()='anchor tag text']"));

または By.linkText("anchor tag text"); で要素を見つけます。

       iwe.FindElements(By.linkText("anchor tag text"));
于 2013-07-09T09:46:07.443 に答える
-1

これを使ってみてください。Elements がページ内に存在するのを待ちます。

static void ForElementsArePresent(IWebDriver wd, double waitForSeconds, By by)
        {
            try
            {
                WebDriverWait wait = new WebDriverWait(wd, TimeSpan.FromSeconds(waitForSeconds));
                wait.Until(x => (x.FindElements(by).Count > 0));
            }
            catch (WebDriverTimeoutException)
            {
                Console.WriteLine("Waiting for element " + by + " to disappear timed out after " + waitForSeconds + " seconds.");
                throw;
            }                       
        }
于 2013-05-17T11:57:00.270 に答える