gpt4 book ai didi

javascript - 使用 CasperJS 检查 AJAX 加载的 JS 对象/类?

转载 作者:行者123 更新时间:2023-11-27 22:54:58 25 4
gpt4 key购买 nike

我使用与 Checking JavaScript AJAX loaded resources with Mink/Zombie in PHP? 中相同的示例:

<强> test_JSload.php

<?php
if (array_key_exists("QUERY_STRING", $_SERVER)) {
if ($_SERVER["QUERY_STRING"] == "getone") {
echo "<!doctype html>
<html>
<head>
<script src='test_JSload.php?gettwo'></script>
</head>
</html>
";
exit;
}

if ($_SERVER["QUERY_STRING"] == "gettwo") {
header('Content-Type: application/javascript');
echo "
function person(firstName) {
this.firstName = firstName;
this.changeName = function (name) {
this.firstName = name;
};
}
";
exit;
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style type="text/css">
.my_btn { background-color:yellow; }
</style>
<script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
var thishref = window.location.href.slice(0, window.location.href.indexOf('?')+1);
var qstr = window.location.href.slice(window.location.href.indexOf('?')+1);

function OnGetdata(inbtn) {
console.log("OnGetdata; loading ?getone via AJAX call");
//~ $.ajax(thishref + "?getone", { // works
var ptest = {}; // init as empty object
console.log(" ptest pre ajax is ", ptest);

$.ajax({url: thishref + "?getone",
async: true, // still "Synchronous XMLHttpRequest on the main thread is deprecated", because we load a script; https://stackoverflow.com/q/24639335
success: function(data) {
console.log("got getone data "); //, data);
$("#dataholder").html(data);
ptest = new person("AHA");
console.log(" ptest post getone is ", ptest);
},
error: function(xhr, ajaxOptions, thrownError) {
console.log("getone error " + thishref + " : " + xhr.status + " / " + thrownError);
}
});

ptest.changeName("Somename");
console.log(" ptest post ajax is ", ptest);
}

ondocready = function() {
$("#getdatabtn").click(function(){
OnGetdata(this);
});
}
$(document).ready(ondocready);
</script>
</head>


<body>
<h1>Hello World!</h1>

<button type="button" id="getdatabtn" class="my_btn">Get Data!</button>
<div id="dataholder"></div>
</body>
</html>

然后,您可以使用 PHP > 5.4 CLI(命令行)在同一目录(.php 文件)中运行临时服务器:

php -S localhost:8080

...最后,您可以访问http://127.0.0.1:8080/test_JSload.php页面.

简单地说,在这个页面中,当单击按钮时,JavaScript 类会分两遍加载 - 首先是一个带有 <script> 的 HTML。标签,其脚本将在第二遍中加载。 Firefox 会在控制台中打印此操作:

OnGetdata; loading ?getone via AJAX call      test_JSload.php:13:3
ptest pre ajax is Object { } test_JSload.php:16:3
TypeError: ptest.changeName is not a function test_JSload.php:31:3
got getone data test_JSload.php:21:7
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help http://xhr.spec.whatwg.org/ jquery-1.12.4.min.js:4:26272
ptest post getone is Object { firstName: "AHA", changeName: person/this.changeName(name) } test_JSload.php:24:7

我最终想检查 ptest变量或 person CasperJS 中的类。到目前为止,我制作了这个脚本:

<强> test_JSload_casper.js

// run with:
// ~/.nvm/versions/node/v4.0.0/lib/node_modules/casperjs/bin/casperjs test_JSload_casper.js
// based on http://code-epicenter.com/how-to-login-to-amazon-using-casperjs-working-example/

var casper = require('casper').create({
pageSettings: {
loadImages: false,//The script is much faster when this field is set to false
loadPlugins: false,
userAgent: 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36'
}
});

//First step is to open page
casper.start().thenOpen("http://127.0.0.1:8080/test_JSload.php", function() {
console.log("website opened");
});

//Second step is to click to the button
casper.then(function(){
this.evaluate(function(){
document.getElementById("getdatabtn").click();
});
});

//Wait for JS to execute?!, then inspect
casper.then(function(){
console.log("After login...");
console.log("AA " + JSON.stringify(person));
});

casper.run();

...但是,当我运行这个 CasperJS 脚本时,我得到的是:

$ ~/.nvm/versions/node/v4.0.0/lib/node_modules/casperjs/bin/casperjs test_JSload_casper.js
website opened
After login...

...没有别的了。请注意最后一行 console.log("AA " + JSON.stringify(person));甚至部分不执行(即,没有打印“AA”,也没有任何类型的错误消息)。

那么,是否可以使用 Casper JS 来检查此类资源(AJAX 加载的 JS 对象/类,可能通过多次运行/步骤加载) - 如果可以,如何进行?

最佳答案

通过点击触发的 Ajax 请求可能没有足够的时间对您正在抓取的页面产生影响。确保使用众多 wait* 函数之一等待其完成。如果 DOM 由于 Ajax 请求而发生更改,那么我建议 waitForSelector .

一个相关问题是页面的 JavaScript 已损坏。由于填充 ptest 的 Ajax 请求是异步的,因此 ptest.changeName("Somename") 在响应到达之前执行,从而导致 TypeError。您可以将 ptest.changeName(...) 移至 Ajax 请求的 success 回调。

为了从页面查看控制台消息,您必须收听 'remote.message' event :

casper.on("remote.message", function(msg){
this.echo("remote> " + msg);
});

casper.start(...)...

关于javascript - 使用 CasperJS 检查 AJAX 加载的 JS 对象/类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37697645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com