【发布时间】: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