使用VBA在提交表单时操作IE(文本区域字段出现问题)



你好StackOverflow社区,

我有一个关于使用VBA操作IE的问题。

该代码是关于在网站的形式中插入数据的。到目前为止,除了以下"文本区域"字段外,一切都很顺利:

<textarea cols="1" rows="1" id="id_content" name="name_content" class="htmleditor">Content of the textarea is displayed here</textarea>

除了实际字段(如上所述,它来自类型文本区域)之外,还有更多来自类型按钮的元素,它们包含在htmleditor中。

我想做的恰恰是更改文本字段中的文本。因此,我希望能够写一些不同的东西,而不是"文本区域的内容显示在这里"。如果可能的话,也可以更改格式,例如一些粗体部分等。然后提交表格。

到目前为止我使用的代码:

Dim IEApp As SHDocVw.InternetExplorer
Set IEApp = New SHDocVw.InternetExplorer
Dim htmlDocument As htmlDocument
Dim objNotes As Object
Dim strNotes As String
With IEApp
    IEApp.Visible = True
    IEApp.Navigate "..."
    Do: Loop Until IEApp.Busy = False
    Do: Loop Until IEApp.document.ReadyState = "complete"
End With
Set objNotes = IEApp.document.getElementsByName("name_content")(0)
MsgBox objNotes.innerText ' 1)
MsgBox objNotes.innerHTML ' 2)
MsgBox objNotes.outerHTML ' 3)
MsgBox objNotes.Value ' 4)
objNotes.Value = "Hello World" 'Here I want to change the text in the textfield
MsgBox objNotes.innerText ' 5)
MsgBox objNotes.innerHTML ' 6)
MsgBox objNotes.outerHTML ' 7)
MsgBox objNotes.Value ' 8)
IEApp.document.forms(0).submit

MsgBox命令提供以下

1) <p>Content of the textarea is displayed here</p>
2) &lt;p&gt;Content of the textarea is displayed here&lt;/p&gt;
3) <textarea name="name_content" class="htmleditor" id="id_content" aria-hidden="true" style="display: none;" rows="1" cols="1">&lt;p&gt;Content of the textarea is displayed here&lt;/p&gt;</textarea>
4) <p>Content of the textarea is displayed here</p>
5) <p>Hello World</p>
6) &lt;p&gt;Hello World&lt;/p&gt;
7) <textarea name="name_content" class="htmleditor" id="id_content" aria-hidden="true" style="display: none;" rows="1" cols="1">&lt;p&gt;Hello World&lt;/p&gt;</textarea>
8) <p>Hello World</p>

但在成功提交表单后,页面不会更改。

到目前为止我尝试了什么:

  • getElementByID而不是getElementsbyName
  • 通过浏览网站的所有元素并直接引用它来搜索此元素
  • 更改innerText、innerHTML、outerHTML、value的值
  • 由于ariaHidden=True试图通过VBA更改此内容,但我找不到合适的方法
  • 将一个新的textArea变量定义为Object/HTMLTextAreaElement/HTMLRichtextElement/HTMLInputTextElement/HTMLAreaElement,并通过此更改值,但没有任何数据类型影响它
  • 将公式设置为活动,对其进行聚焦,但不能使用SendKeys函数以这种方式编写文本

你知道还能做什么吗?

对于TextArea,不要使用byid或byname,而是使用byTagName我认为您必须使用函数getElementsbyTagName

最新更新