[英]PhantomJS error: Cannot access member 'write' of deleted QObject
[英]Error: cannot call function of deleted QObject
我有一个网页,点击一个按钮打开一个弹出窗口。 新弹出窗口有另一个按钮,点击弹出按钮后弹出窗口关闭,父窗口重新加载到新页面。
如果我不重新加载我的父窗口,我会得到
FAIL Error: cannot access member `evaluate' of deleted QObject
# type: uncaughtError
# error: "Error: cannot access member `evaluate' of deleted QObject"
# merge: undefined
Error: cannot access member `evaluate' of deleted QObject
如果我重新加载,没有这样的错误,但casperjs脚本只是卡住而没有执行进一步的步骤。
这是我的要求我想点击open popup
,然后再次弹出窗口加载后点击close me
按钮。 当父窗口重新加载时,获取重新加载页面的内容。
这是我的代码
1.主页:
<?php
session_start();
$newPage = isset($_SESSION['newpage']) ? $_SESSION['newpage'] : "";
?>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
setTimeout(function() {
console.log("NEW PAGE URL = <?php echo $newPage; ?>");
if ("<?php echo $newPage; ?>" != "") {
location = "<?php echo $newPage; ?>";
}
}, 10000);
});
</script>
<button onclick="window.open('popup.php');">Click me</button>
2.popup页面:
<?php
session_start();
$_SESSION['newpage'] = 'newpage.php';
echo $_SESSION['newpage'];
?>
<head>
<title>Popup</title>
</head>
<button onclick="window.close();window.opener.location.reload();" class="popupbutton">Close me</button>
3.新页面(重新加载页面):
<?php
session_start();
$_SESSION['newpage'] = '';
?>
<head>
<title>Newpage</title>
</head>
This is new page.
<button onclick="location = 'index.php';" class="popupbutton">Go back</button>
我尝试过与https://github.com/n1k0/casperjs/issues/644相同,但它没有用。 所以,我修改了代码如下:这不是没有给出预期的结果。
Casper脚本
var casper = require('casper').create({
verbose: true,
logLevel: 'debug'
});
var popupClosed = false;
var popupOpened = false;
casper.on('popup.closed', function() {
this.echo("POPUP CLOSED");
popupClosed = true;
this.log(casper.popups.length);
this.echo("Page: " + this.getTitle());
this.echo("URL = " + this.getCurrentUrl());
this.then(function() {
this.echo(this.evaluate(function() {
return document.documentElement.innerHTML;
}));
});
this.open('http://localhost/sample-app/index.php');
this.then(function() {
this.echo(this.getPageContent());
})
});
casper.on('navigation.requested', function() {
this.echo("===" + this.getCurrentUrl() + this.getCurrentUrl().indexOf("newpage") > -1);
casper.echo(this.getPageContent());
if (this.getCurrentUrl().indexOf("newpage") > -1) {
casper.then(function() {
this.echo(this.evaluate(function() {
return document.documentElement.innerHTML;
}));
});
}
});
casper.start('http://localhost/sample-app/index.php');
casper.then(functi
on() {
this.waitForSelector("button", function() {
this.echo("URL = " + this.getCurrentUrl());
this.thenClick("button", function() {
this.echo("click to open popup");
});
casper.waitForPopup(/popup\.php/gi, function() {
popupClosed = false;
popupOpened = true;
});
casper.waitFor(function() {
return popupOpened;
}, function() {
casper.withPopup(/popup\.php/gi, function() {
this.echo("Popup url: " + this.getTitle() + ", " + this.getCurrentUrl());
this.waitForSelector("button.popupbutton", function() {
this.click("button.popupbutton");
}, function() {
this.log("No button on popup");
});
});
}, function() {
this.echo("Wait TIMEOUT");
}, 60000);
casper.then(function() {
this.echo("Its here...");
this.waitFor(function() {
return popupClosed;
}, function() {
this.echo("Page: " + this.getTitle());
}, function() {
this.echo("TIME OUT");
}, 60000);
});
}, function() {
});
this.log("COMPLETED");
});
casper.run(function() {
this.exit();
});
版本:phantomjs -1.9 casperjs - 1.0.2。
您的casperjs代码存在多个问题。
第一:从不调用casper.on
事件处理程序,因为您实际上从未发出任何事件。
所以每当你设置popupClosed = true;
你也必须打电话
casper.emit("popup.closed");
你还必须调用casper.emit("navigation.requested");
它有意义(不在evaluate
块内)。
第二步: casper.then
函数和所有casper.wait*
创建一个步骤,该步骤将在每个其他当前计划的步骤之后进行调度。
其他已安排的步骤可以包含页面转换,如点击或表单提交,在这种情况下,新安排的步骤将在错误的页面上执行。 看来你的错误deleted QObject
直接转换为这种情况。
这意味着,如果您等待弹出窗口显示以下函数,则应该减少嵌套或嵌套所有内容。
你的代码有执行索引:
casper.waitFor(function() {
return popupOpened;
}, function() {
// executed at position 1
casper.withPopup(/popup\.php/gi, function() {
// executed at position 3
this.echo("Popup url: " + this.getTitle() + ", " + this.getCurrentUrl());
this.waitForSelector("button.popupbutton", function() {
// executed at position 4
this.click("button.popupbutton");
}, function() {
this.log("No button on popup");
});
});
}, function() {
this.echo("Wait TIMEOUT");
}, 60000);
casper.then(function() {
// executed at position 2
});
解决方案1: Unnest,但这对弹出上下文不起作用,取决于静态等待和硬退出:
casper.waitFor(function() {
return popupOpened;
}, function() {
// executed at position 1
}, function() {
this.echo("Wait TIMEOUT");
this.exit;
}, 60000);
casper.withPopup(/popup\.php/gi, function() {
// executed at position 2
this.then(function(){
this.echo("Popup url: " + this.getTitle() + ", " + this.getCurrentUrl());
});
this.waitForSelector("button.popupbutton");
this.thenClick("button.popupbutton");
});
casper.then(function() {
// executed at position 3
});
编辑:看起来,没有必要等待一段时间。 您也可以使用waitFor*
函数,但不要进一步嵌套。 看看这个天真的方法工作的要点 。
解决方案2:让每一步都越来越深。 这不适用于弹出窗口,因为上下文无法在更深层次的步骤中切换回页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.