Casperjs getelementbyid工作,但getelementbyname不工作



好的,我正在尝试填写没有表单的HTML页面上的输入字段,我正在尝试围绕特定原因的工作,只需要按照我尝试的方式去做。

这就是我的HTML
    <tr>
    <td valign="top" nowrap width="120">Amount: <font size="2" color="#CC0000">*</font>    </td>
    <td width="490">
    <input type="text" name="data" size="20" maxlength="20" value="">
    <select name="Percent"><option value="2" >NUM</option><option value="1" >num2</option><option value="3" selected>resultin</option></select>
    <div class="error"></div>
    </td>
    </tr>
   <td width="490">
      <input type="text" name="account" id="account" size="20" maxlength="20" value="">
    </td>

现在当我用这个插入文本到account字段时一切都正常

if(casper.exists(ac1)){
var uel = "https://example.com";
this.thenOpen(uel, function() {
casper.wait(10000, function() {    
  casper.then(function() {
    this.evaluate(function() {
      document.getElementById('account').value = '345de';

    });
casper.wait(10000, function() {
  casper.then(function() {
    this.capture("filenadfgmedsfg.jpg");
    var el2 = this.getHTML();
    fs.write('results23.html', el2, 'w');
});
});
});

但是当我尝试下面的任何一个来填充输入数据字段时,我不能让它工作

if(casper.exists(ac1)){
var uel = "https://example.com";
this.thenOpen(uel, function() {
casper.wait(10000, function() {    
  casper.then(function() {
    this.evaluate(function() {
      document.getElementById('data').value = '345de';

    });
casper.wait(10000, function() {
  casper.then(function() {
    this.capture("filenadfgmedsfg.jpg");
    var el2 = this.getHTML();
    fs.write('results23.html', el2, 'w');
});
});
});

和这个不工作,请帮我修复这个

if(casper.exists(ac1)){
var uel = "https://example.com";
this.thenOpen(uel, function() {
casper.wait(10000, function() {    
  casper.then(function() {
    this.evaluate(function() {
      document.getElementByName('data').value = '345de';

    });
casper.wait(10000, function() {
  casper.then(function() {
    this.capture("filenadfgmedsfg.jpg");
    var el2 = this.getHTML();
    fs.write('results23.html', el2, 'w');
});
});
});    

谁能告诉我一种用类似getElementBy技术的方法填充数据字段的方法

没有getElementByName()方法。您可能想要的是getElementsByName(),它返回元素列表,而不仅仅是单个元素,因此您必须要么只获取列表中的第一项,要么处理整个列表(取决于您想要做什么)。

var uel = "https://example.com";
this.thenOpen(uel, function () {
    casper.wait(10000, function () {
        casper.then(function () {
            this.evaluate(function () {
                document.getElementsByName('data')[0].value = '345de';
            });
        });
    });
});

最新更新