gpt4 book ai didi

javascript - casperjs getelementbyid 工作但 getelementbyname 不工作

转载 作者:行者123 更新时间:2023-11-27 23:58:29 26 4
gpt4 key购买 nike

好的,我正在尝试在没有表单的 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>

现在,当我使用它在帐户字段中插入文本时,一切正常

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';
});
});
});
});

关于javascript - casperjs getelementbyid 工作但 getelementbyname 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22585072/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com