【问题标题】:How to get current HTML source of AJAX page using facebook WebDriver?如何使用 facebook WebDriver 获取 AJAX 页面的当前 HTML 源代码?
【发布时间】:2015-04-30 20:05:39
【问题描述】:

您好,我是 facebook Webdriver 新手。我需要有关获取 AJAX 页面的 HTML 源代码的帮助。

这是我的预期结果:

$first == HTML source of the 1st page.
$second == HTML source of the 2nd page.
$third == HTML source of the 3rd page.

但我的输出

$first == HTML source of the 1st page.
$second == $first
$third == HTML source of the 2nd page.

但是,当我登陆第三页时,我可以获得第二页的 HTML 源代码。 我不知道为什么我无法在当前页面上获取当前 HTML。

请帮忙!

这是我的代码:

<?php 
$host = 'http://localhost:4444/wd/hub'; 
$capabilities = DesiredCapabilities::firefox();
$driver = RemoteWebDriver::create($host, $capabilities, 5000);

// Openning page
$driver->get('https://careers.yahoo.com/?global=1');

// Click 'Search' 
$driver->findElement(WebDriverBy::className('yellow-submit'))->click();

// Wait until Ajax part loaded
$driver->wait(40)->until(
 WebDriverExpectedCondition::presenceOfAllElementsLocatedBy(
 WebDriverBy::className('actions-container')
));

// Print HTML of the 1st page
$first = $driver->getPageSource();
print_r($first);

// go to 2nd page
$driver->findElement(WebDriverBy::id('next'))->click();

// Wait until the 2nd page is loaded
$driver->wait(40)->until(
 WebDriverExpectedCondition::presenceOfAllElementsLocatedBy(
 WebDriverBy::className('actions-container')
));

// Print HTML of the 2nd page
$second = $driver->getPageSource();
print_r($second);

// go to 3rd page
$driver->findElement(WebDriverBy::id('next'))->click();

// Wait until the 3rd page is loaded
$driver->wait(40)->until(
 WebDriverExpectedCondition::presenceOfAllElementsLocatedBy(
 WebDriverBy::className('actions-container')
));

// Print HTML of the 3rd page
$second = $driver->getPageSource();
print_r($third);

$driver->quit();

【问题讨论】:

  • 你确定等待真的有效吗?如果我是你,我最好等到第一页的某些内容变得陈旧,以确保它们被卸载
  • 您的意思是我必须等待更长的时间才能获得 $second 的 getPageSource()?当我 print_r($second);我错了吗?
  • 不,增加更长的等待时间并不能解决这个问题,因为当满足预期条件时,您的等待就结束了。我想说的是,你应该等到第一个表的内容被卸载,然后等待新表的加载
  • 非常感谢您回答我的问题!感谢您的回复时间和精力。如果有机会,您能建议任何示例链接吗?
  • satlenessOf 条件为here。当元素不再附加到 DOM 时,它返回 true。因此,您可以尝试等到某些表格元素被卸载,然后 - 直到新元素被加载

标签: php ajax selenium webdriver


【解决方案1】:

"...If the page has been modified after loading (for example, by Javascript) there is no guarantee that the returned text is that of the modified page..."(getPageSource 部分)

最终这意味着 selenium 不一定拥有最新的来源!在给定的时间。

对于您的结果:循环很好,请尝试在搜索元素之间休息片刻。它将节省测试不需要的睡眠时间,但仍然不会中断,

【讨论】:

  • 感谢您的回复。美好的一天!
【解决方案2】:

在 JQuery 中,一旦所有 Ajax 完成,就有一个标志 JQuery.active = 0。这在非 JQuery AJAX 中是否属实我不知道。

以下代码是我从这里的某人那里偷来的,我这辈子都不记得在哪里了,但它很方便(在 JQuery + Selenium2 + PHPUnit 的上下文中)

public function waitForAjax()
{
    while(true)
    {
        $ajaxIsComplete = array(
            'script' => 'return jQuery.active == 0',
            'args' => array()
        );
        $ajaxIsComplete = $this->execute($ajaxIsComplete);
        if ($ajaxIsComplete) {
            break;
        }
        sleep(1);
    }
}

而不是仅仅在 n 是任意数字的所有内容上都粘贴“sleep(n)”,毕竟不用每次都等那么久是件好事。 ..

【讨论】:

  • “我不知道这在非 JQuery AJAX 中是否正确。”对于任何不是通过 jQuery 发起的 AJAX 来说,这都是不正确的。
  • 添加了 sleep(1) 以在其中暂停 1 秒。否则它可能会占用服务器资源/不必要地写入大量日志文件。
  • “我从这里的某人那里偷来的以下代码,我一辈子都不记得在哪里了”让我们解开一个 6 年前的谜团:stackoverflow.com/a/30069368/4306828 使用以下 google 搜索找到; site:stackoverflow.com "$ajaxIsComplete = array(" 其中 "site:site.com" 将搜索限制在给定站点,并且以下术语上的双引号进行精确搜索。只有与引号之间的字符完全匹配的结果才是返回。对于这个特定的搜索,只有两个结果,你的,还有一个比你早 15 天的答案)希望这会有所帮助哈哈
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-24
  • 2011-12-11
  • 2012-07-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多