如何使用PhantomJS更改选择值 [英] How to Change select value using PhantomJS
本文介绍了如何使用PhantomJS更改选择值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在节点(节点模块)内部使用PhantomJS制作了一个刮板.
I made a scraper using PhantomJS inside node (Node Module).
我正在尝试从页面(url)上的表中获取数据. 页面加载时,仅显示表的25条记录.底部有一个选择",您可以将其更改为全部"以查看所有记录.如何在返回HTML之前将select的值更改为全部"?
I am trying to get data from a table on the page (url). When the page loads it only displays 25 records of the table. There is a 'select' at the bottom that you can change to 'All' to see all records. How can i change the value of the select to 'All' before getting the HTML returned?
var phantom = require('phantom');
phantom.create().then(function(ph){
ph.createPage().then(function(page){
page.open(url).then(function(status){
console.log(status);
page.property('content').then(function(content){
console.log(content);
page.close();
ph.exit();
});
});
});
});
<select name="qs-rankings_length" aria-controls="qs-rankings" class="jcf-hidden">
<option value="100">100</option>
<option value="200">200</option>
<option value="-1">All</option>
</select>
推荐答案
这就是我的方法.
page.evaluate(function() {
$('#select_element_selector').val('All').change();
});
我假设您的页面上有jQuery.
I'm assuming that you have jQuery on the page.
或者,没有jQuery:
Or, without jQuery:
page.evaluate(function() {
document.getElementById('select').selectedIndex = 0;
// or use document.getElementById('select').value = 'All';
}
这篇关于如何使用PhantomJS更改选择值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文