6

Phantomjs で Selenium を使用しており、ページが完全に読み込まれた後にページ コンテンツを取得したいと考えています。

http://docs.seleniumhq.org/docs/04_webdriver_advanced.jspを試しましたが、phantomjs では動作しないようです

明示的な待機:

using (IWebDriver driver = new PhantomJSDriver())
{
    IWait<IWebDriver> wait = new OpenQA.Selenium.Support.UI.WebDriverWait(driver, TimeSpan.FromSeconds(30.00));
    wait.Until(driver1 => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"));

    driver.Navigate().GoToUrl(url);

    content = driver.PageSource;

    driver.Quit();
}

別のテスト:

using (IWebDriver driver = new PhantomJSDriver())
{
    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));

    driver.Url = url;

    IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
        {
            return d.FindElement(By.Id("footer")); // failed because it's not yet loaded full content 
        });

    content = driver.PageSource;
}

または暗黙の待機:

using (IWebDriver driver = new PhantomJSDriver())
{
    driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));
    driver.Navigate().GoToUrl(url);

    content = driver.PageSource;

    driver.Quit();
}

まだまだ内容が足りない。唯一の方法は、Thread.Sleep(waitTime); を置くことです。これは良い解決策ではありません。

ありがとう。

4

3 に答える 3

3

拡張メソッドを作成しました。このメソッドでは、条件を設定できます。

  public static bool WaitUntil(this IWebDriver driver, Func<IWebDriver, bool> expression, int timeOutSeconds = 10)
    {

        TimeSpan timeSpent = new TimeSpan();

        bool result = false;
        while (timeSpent.TotalSeconds < timeOutSeconds)
        {

            result = expression.Invoke(driver);

            if (result == true)
            {
                break;
            }
            Thread.Sleep(timeSleepingSpan);
            timeSpent = timeSpent.Add(new TimeSpan(0, 0, 0, 0, timeWaitingSpan));

        }
        return result;

    }

お気に入り

       driver.WaitUntil(d => d.Url.Equals("https://www.meusite.com/"));
于 2014-09-09T12:57:45.647 に答える
1

次のようなことを試してください:

try (
  ExpectedConditions.presenceOfElementLocatedBy
  ExpectedConditions.visibilityOfElementLocatedBy
) catch error if both conditions are not met
于 2014-04-08T14:46:36.283 に答える