【问题标题】:How to: Print Select Option Values Using CasperJS?如何:使用 CasperJS 打印选择选项值?
【发布时间】:2016-11-14 10:10:17
【问题描述】:

我正在尝试打印“complist”中每个选项的值,如下所示:

<select name="complist">
    <option selected="" value="111 1">107-83-5&nbsp;&nbsp;C6H14&nbsp;&nbsp;2-Methylpentane</option>
    <option value="1 24">&nbsp;75-07-0&nbsp;&nbsp;C2H4O&nbsp;&nbsp;&nbsp;&nbsp;Acetaldehyde</option>
    <option value="6 2">106-93-4&nbsp;&nbsp;C2H4Br2&nbsp;&nbsp;Ethylene bromide</option>
</select>

我试过了:

casper.then(function(){
    this.evaluate(function(){
        //var options = document.querySelectorAll('select[name="complist"]'); 
        var options = document.querySelector('select[name="complist"]');
    })
    for (var i=0; i< options.length; i++){
        console.log(">>> " + options[i].textContent);
    }
});

但我收到以下错误:

  • 错误> ReferenceError:找不到变量:选项,错误
  • 文件> phantomjs://code/gruppen3.js,警告
  • 行> 113,警告
  • 功能>,警告

由 for 循环中的 options.length 产生:/

我尝试了其他几种方法(例如How to select an option with CasperJS)来返回选项列表,但到目前为止没有任何效果。

要确切了解我在说什么,您可以:

  1. 导航到http://www.ddbst.com/unifacga.html
  2. 选择选项编号。 4(DDB 搜索)
  3. 输入例如174 作为 ddb 编号并提交。
  4. 您现在应该会看到一个“complist”,其中只有一个水选项。

如何返回选项值?任何帮助将不胜感激。

【问题讨论】:

    标签: javascript list phantomjs casperjs options


    【解决方案1】:

    我已经测试过了,它可以工作!

    var casper = require('casper').create({verbose: true,logLevel: "debug"});
    casper.start('http://www.ddbst.com/unifacga.html',function(){
    this.withFrame('ircframe',function(){
    this.click('input[id="ID4"]');
    
    this.wait(0,function(){
    this.fillSelectors('form[name="opeform"]', {'input[name="ddbnumber"]': '155'},false);
    });
    this.wait(0,function(){this.click('input[name="search_ddbnum"]');});
    this.wait(0,function(){
    var i,o=this.fetchText('select[name="complist"]');
    console.log("The value is: "+o)
    });
    });
    });
    casper.then(function(){this.capture('test.png');});
    casper.run();
    

    您可以通过以下方式运行它:
    ./casperjs --web-security=no test.js &gt;&gt;/dev/stdout

    【讨论】:

    • 更新:now faster x2!
    • 看看my latest work - 这非常有趣,但很难!
    • 我建议您将此答案标记为解决方案,让人们知道它确实有效。我很高兴处理它,iframe 真的很有趣!
    • 再次感谢!你肯定给了我一些思考的食物,例如它比我现在的紧凑得多(很多)。然而,作为一个新手,map 过程对我来说似乎更直观(这是我的第一个 casperJS 项目,或者是 JS)。不幸的是,我无法标记该问题的两个解决方案...
    • @Igor,为了让你的答案更好,不要只提供代码,尝试显示错误并告诉你如何做对。此外,缩进良好的示例更容易理解。
    【解决方案2】:

    casper.evaluate 在沙箱内执行 javascript。您必须专门从该函数返回数据,因为在那里创建的变量将不存在于主脚本中。正确的实现:

    /// MAIN SCRIPT CONTEXT
    casper.then(function(){
    
        // Value which is returned from sandbox must be saved in some local variable
        var localOptions = this.evaluate(function(){
            // SANDBOX CONTEXT -------------------------------
    
            // Dont return objects. Return arrays, strings, numbers
            var options = [];
            options = [].map.call(document.querySelectorAll('select[name="complist"] option'), function(option){
                return option.innerText;
            });
            // You have to return scraped data back to the outer script
            return options;
    
            // END OF SANDBOX CONTEXT -----------------------
    
        });
    
        console.log(">>> " + localOptions);
        // just "options" array does not exist here    
    });
    

    【讨论】:

    • 首先有效;谢谢 :) 我想我基本上什么都懂;但是,如果有人有指针,我会用我自己的话写出这些步骤: 1. “then” 在 casperJS 框架内创建一个步骤。 2.“评估”在沙箱中打开 DOM(来自 phantomJS?)。 3.“map”类(我假设是因为点)不是循环选项对象,而是用于将“option”值加载到名为“options”的数组中。
    • 这里 ", function(option)" 在调用中使用,但如果选择器用于图像,我们可以使用 ", function(img)"?简而言之,我想我理解语义,但我怎样才能学习在其他(类似)问题中使用这个答案所需的正确语法? 4. 然后将选项列表返回到外部脚本(“then”步骤),将其打印到控制台...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-15
    相关资源
    最近更新 更多