嗨,我是一名facebook网络驱动程序新手。我需要帮助获得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();
"…如果页面在加载后被修改(例如,通过Javascript),则不能保证返回的文本是修改后的页面的文本…"(getPageSource部分)
最终,这意味着硒不一定是最新的来源!在给定的时间。
对于你的结果:循环很好,在搜索元素之间试着睡一会儿。它将为测试节省不必要的睡眠时间,但仍然不会中断,
在JQuery中,有一个标志,一旦所有Ajax都完成,JQuery.active=0。我不知道这在非JQueryAJAX中是否成立。
下面的代码是我从这里的某个人那里偷来的,我一辈子都记不清在哪里了,但它很方便(在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是任意数字的所有东西上贴上"睡眠(n)",毕竟每次都不用等那么久是件好事。。。