您好我是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.
然而,当我登陆第3页时,我可以获得第2页的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();
答案 0 :(得分:0)
最终,这意味着硒不一定拥有最新的来源!在给定的时间。
对于你的结果:循环是好的,尝试在搜索元素之间短暂休息。它会保存测试不需要的睡眠时间,但仍然不会中断,
答案 1 :(得分:0)
在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 )”粘贴在 n 是任意数字的所有内容上,毕竟不是每次都要等待那么长时间。 ..