casperjs get开头的几个dom操作使用

mac2022-06-30  25

getCurrentUrl()

Signature: getCurrentUrl()

Retrieves current page URL. Note that the url will be url-decoded:

获得当前页面的URL,注意这个URL是已经解码过的

casper.start('http://www.google.fr/', function() { this.echo(this.getCurrentUrl()); // "http://www.google.fr/" }); casper.run();
getElementAttribute()

Signature: getElementAttribute(String selector, String attribute)

新增于1.0版本

Retrieves the value of an attribute on the first element matching the provided selector:

获取选择器匹配的第一个元素的属性值

var casper = require('casper').create(); casper.start('http://www.google.fr/', function() { require('utils').dump(this.getElementAttribute('div[title="Google"]', 'title')); // "Google" }); casper.run();  
getElementsAttribute()

Signature: getElementsAttribute(String selector, String attribute)

New in version 1.1.

获取选择器匹配的所有元素的属性值

var casper = require('casper').create(); casper.start('http://www.google.fr/', function() { require('utils').dump(this.getElementsAttribute('div[title="Google"]', 'title')); // "['Google']" }); casper.run();
getElementBounds()

Signature: getElementBounds(String selector)

获取选择器匹配的元素的区域

它返回一个对象,包括以下键:top, left, width 和 height,如果没有匹配到元素,则返回null

var casper = require('casper').create(); casper.start('http://www.google.fr/', function() { require('utils').dump(this.getElementBounds('div[title="Google"]')); }); casper.run(); 将会得到以下输出: { "height": 95, "left": 352, "top": 16, "width": 275 }
getElementsBounds()

Signature: getElementsBounds(String selector)

新增于1.0版本

获取选择器匹配的所有元素的区域信息的数组

它返回一个对象数组,对象包括以下四个键:top, left, width 和 height

getElementInfo()

Signature: getElementInfo(String selector)

New in version 1.0.

Retrieves information about the first element matching the provided selector:

获取选择器匹配的第一个元素的信息

casper.start('http://google.fr/', function() { require('utils').dump(this.getElementInfo('#hplogo')); }); 得到以下结果: { "attributes": { "align": "left", "dir": "ltr", "id": "hplogo", "onload": "window.lol&&lol()", "style": "height:110px;width:276px;background:url(/images/srpr/logo1w.png) no-repeat", "title": "Google" }, "height": 110, "html": "<div nowrap=\"nowrap\" style=\"color:#777;font-size:16px;font-weight:bold;position:relative;left:214px;top:70px\">France</div>", "nodeName": "div", "tag": "<div dir=\"ltr\" title=\"Google\" align=\"left\" id=\"hplogo\" οnlοad=\"window.lol&&lol()\" style=\"height:110px;width:276px;background:url(/images/srpr/logo1w.png) no-repeat\"><div nowrap=\"nowrap\" style=\"color:#777;font-size:16px;font-weight:bold;position:relative;left:214px;top:70px\">France</div></div>", "text": "France\n", "visible": true, "width": 276, "x": 62, "y": 76 } 提示: 这个方法返回的不是一个dom对象,它只是对象的一个描述,因为casper环境里是不能直接获取页面里的对象的  
getElementsInfo()

Signature: getElementsInfo(String selector)

New in version 1.1.

获取选择器匹配的所有元素的信息

casper.start('http://google.fr/', function() { require('utils').dump(this.getElementsInfo('#hplogo')); }); 得到以下结果: [ { "attributes": { "align": "left", "dir": "ltr", "id": "hplogo", "onload": "window.lol&&lol()", "style": "height:110px;width:276px;background:url(/images/srpr/logo1w.png) no-repeat", "title": "Google" }, "height": 110, "html": "<div nowrap=\"nowrap\" style=\"color:#777;font-size:16px;font-weight:bold;position:relative;left:214px;top:70px\">France</div>", "nodeName": "div", "tag": "<div dir=\"ltr\" title=\"Google\" align=\"left\" id=\"hplogo\" οnlοad=\"window.lol&&lol()\" style=\"height:110px;width:276px;background:url(/images/srpr/logo1w.png) no-repeat\"><div nowrap=\"nowrap\" style=\"color:#777;font-size:16px;font-weight:bold;position:relative;left:214px;top:70px\">France</div></div>", "text": "France\n", "visible": true, "width": 276, "x": 62, "y": 76 } ] 这个方法返回的不是一个nodelist,它只是对象的列表的一个描述,因为casper环境里是不能直接获取页面里的对象的  
getFormValues()

Signature: getFormValues(String selector)

新增于1.0版本

获取一个给定的表单的所有字段值:

casper.start('http://www.google.fr/', function() { this.fill('form', {q: 'plop'}, false); this.echo(this.getFormValues('form').q); // 'plop' }); casper.run();
getGlobal()

Signature: getGlobal(String name)

通过名称,获取远程DOM环境下的一个全局变量的值,基本上,getGlobal('foo')意味着将从页面获得window.foo的值:

casper.start('http://www.google.fr/', function() { this.echo(this.getGlobal('innerWidth')); // 1024 }); casper.run();
getHTML()

Signature: getHTML([String selector, Boolean outer])

新增于1.0版本

获取当前页面的HTML代码,默认情况下,它会输出整个页面的html文本

casper.start('http://www.google.fr/', function() { this.echo(this.getHTML()); }); casper.run(); The getHTML() method can also dump HTML contents matching a given selector; for example with this HTML code: getHTML()方法能得到给定的选择器匹配的元素的HTML文本;以下是HTML代码的实例: <html> <body> <h1 id="foobar">Plop</h1> </body> </html> 你可以这样取得它的文本: casper.start('http://www.site.tld/', function() { this.echo(this.getHTML('h1#foobar')); // => 'Plop' }); outer参数允许你得到html格式的文本输出: casper.start('http://www.site.tld/', function() { this.echo(this.getHTML('h1#foobar', true)); // => '<h1 id="foobar">Plop</h1>' });
getPageContent()

Signature: getPageContent()

新增于1.0版本

取得当前页面的文本,用其他文档类型来处理它:

var casper = require('casper').create(); casper.start().then(function() { this.open('http://search.twitter.com/search.json?q=casperjs', { method: 'get', headers: { 'Accept': 'application/json' } }); }); casper.run(function() { require('utils').dump(JSON.parse(this.getPageContent())); this.exit(); });
getTitle()

Signature: getTitle()

获取当前页面的标题

casper.start('http://www.google.fr/', function() { this.echo(this.getTitle()); // "Google" }); casper.run();

转载于:https://www.cnblogs.com/c-x-a/p/7269248.html

相关资源:casperjs网页截屏例子
最新回复(0)