【问题标题】:How to Change select value using PhantomJS如何使用 PhantomJS 更改选择值
【发布时间】:2016-10-25 17:02:53
【问题描述】:

我在节点(节点模块)中使用 PhantomJS 制作了一个刮板。

我正在尝试从页面 (url) 上的表格中获取数据。 当页面加载时,它只显示表的 25 条记录。底部有一个“选择”,您可以将其更改为“全部”以查看所有记录。如何在返回 HTML 之前将 select 的值更改为“All”?

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>

【问题讨论】:

    标签: javascript node.js web-scraping phantomjs web-crawler


    【解决方案1】:

    我就是这样做的。

    page.evaluate(function() {
        $('#select_element_selector').val('All').change();
    });
    

    我假设你的页面上有 jQuery。

    或者,没有 jQuery:

    page.evaluate(function() {
        document.getElementById('select').selectedIndex = 0;
        // or use document.getElementById('select').value = 'All';
    }
    

    【讨论】:

    • 如果我访问的页面有jQuery,我可以这样做吗?我实际上不必为我的脚本“导入”jQuery? (对不起,我是新手)。我在page.property之前做............
    • 我更新了没有 jQuery 的作品的答案,因为我不知道如何使用 phantomjs 导入 jQuery。
    • 还有这个 - TypeError: undefined is not a constructor (evalating 'api.page.len(event.data.pageLength).draw()')
    • 这是选择标签的样子(更新问题)
    猜你喜欢
    • 2015-07-05
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-29
    • 2011-11-22
    • 1970-01-01
    相关资源
    最近更新 更多