【问题标题】:How to set value of an input tag in casperJs如何在 casperJs 中设置输入标签的值
【发布时间】:2013-08-11 12:45:24
【问题描述】:

我有如图所示的输入元素:

<input type="text" class="bg-white" id="couponCode" value="">

如何使用 casperJs 设置/填充其值

【问题讨论】:

    标签: web-scraping phantomjs casperjs


    【解决方案1】:

    【讨论】:

    • 我一直在寻找这个答案一个小时或更长时间。非常感谢。
    • 光标的焦点是否停留在@Srikanth Malyala?
    • 但是,如果没有为该输入提供 id 怎么办? @Srikant Malyala
    • 您可以通过类或某些属性来选择它,例如('.some-btn')
    【解决方案2】:

    有几种不同的方法可用于完成此任务。

    您应该使用casper.sendKeys(),除非您需要执行更复杂的操作。


    casper.sendKeys():

    如果您想从 CasperJS 环境中设置值,并且 input 元素可选form 元素内,那么您可以使用 @ 987654335@:

    casper.sendKeys('#couponCode', 'Hello, world!');
    

    casper.fill():

    如果您想从 CasperJS 环境 中设置值,并且 input 元素 form 元素内,并且 包含 一个name 属性,那么你可以使用casper.fill()

    casper.fill('#form', {
      couponCode: 'Hello, world!', // #form [name="couponCode"]
    });
    

    casper.fillSelectors():

    如果您想从 CasperJS 环境 设置值,并且 input 元素 form 元素内,并且您想引用input 元素使用 CSS3 选择器,那么你可以使用 casper.fillSelectors():

    casper.fillSelectors('#form', {
      '#couponCode': 'Hello, world!', // #form #couponCode
    });
    

    casper.fillLabels():

    如果您想从 CasperJS 环境中设置值,并且 input 元素form 元素内,并且 包含 与文本关联的label 元素,则可以使用casper.fillLabels()

    casper.fillLabels('#form', {
      couponCode: 'Hello, world!', // #form label[text()="couponCode"] input
    });
    

    casper.fillXPath():

    如果您想从 CasperJS 环境 中设置值,并且 input 元素 form 元素内,并且您想引用input 元素使用 XPath 选择器,那么您可以使用 casper.fillXPath():

    casper.fillXPath('#form', {
      '//*[@id="couponCode"]': 'Hello, world!', // #form #couponCode
    });
    

    casper.evaluate():

    如果您想从 Page DOM 环境中设置值,并且 input 元素可选form 元素内,那么您可以使用casper.evaluate():

    casper.evaluate(function () {
      document.getElementById('couponCode').value = 'Hello, world!';
    });
    

    注意:evaluate()类似,您也可以使用:evaluateOrDie()thenEvaluate()thenOpenAndEvaluate()(如果您想同时完成两个或多个相关操作到正在执行的步骤)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-25
      • 1970-01-01
      • 2022-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多