如何等待硒页面重新加载?
How to wait for page reload with selenium?
考虑以下代码:
$this->visit('/');
$text = $this->byCssSelector('.some-element')->text();
$this->byCssSelector('.some-link')->click();
# how do I wait for a new page to be loaded here?
$this->assertEquals($text, $this->byCssSelector('.some-element')->text());
您可以设置(通过 js 调用)window.is_old_page = true
,并检查直到 undefined
从我在网上看到的,尽可能避免执行javascript,所以我想出了非javascript的解决方案:
$this->visit('/');
$text = $this->byCssSelector('.some-element')->text();
$this->waitForPageReload(function () {
$this->byCssSelector('.some-link')->click();
}, 5000);
$this->assertEquals($text, $this->byCssSelector('.some-element')->text());
function waitForPageReload($navigate_f, $timeout) {
$id = $this->byCssSelector('html')->getId();
call_user_func($navigate_f);
$this->waitUntil(function () use ($id) {
$html = $this->byCssSelector('html');
if ($html->getId() != $id)
return TRUE;
}, $timeout);
}
受此启发great article。
在某些情况下您可以使用休眠功能:
sleep($seconds);
考虑以下代码:
$this->visit('/');
$text = $this->byCssSelector('.some-element')->text();
$this->byCssSelector('.some-link')->click();
# how do I wait for a new page to be loaded here?
$this->assertEquals($text, $this->byCssSelector('.some-element')->text());
您可以设置(通过 js 调用)window.is_old_page = true
,并检查直到 undefined
从我在网上看到的,尽可能避免执行javascript,所以我想出了非javascript的解决方案:
$this->visit('/');
$text = $this->byCssSelector('.some-element')->text();
$this->waitForPageReload(function () {
$this->byCssSelector('.some-link')->click();
}, 5000);
$this->assertEquals($text, $this->byCssSelector('.some-element')->text());
function waitForPageReload($navigate_f, $timeout) {
$id = $this->byCssSelector('html')->getId();
call_user_func($navigate_f);
$this->waitUntil(function () use ($id) {
$html = $this->byCssSelector('html');
if ($html->getId() != $id)
return TRUE;
}, $timeout);
}
受此启发great article。
在某些情况下您可以使用休眠功能:
sleep($seconds);