Excel 在IE窗口中填写表单:按按钮并填写文本框
我正在尝试自动填写一个重复和耗时的IE网站 我编写的代码启动IE,让它对用户可见,模糊它需要填写的变量 我找到了文本框的ID和名称,但无法填写 该网站是公司网站,因此我无法附加链接 我可以附加我试图填充的框的HTLM代码和我试图单击的按钮 文本框Excel 在IE窗口中填写表单:按按钮并填写文本框,excel,vba,internet-explorer,web-scraping,Excel,Vba,Internet Explorer,Web Scraping,我正在尝试自动填写一个重复和耗时的IE网站 我编写的代码启动IE,让它对用户可见,模糊它需要填写的变量 我找到了文本框的ID和名称,但无法填写 该网站是公司网站,因此我无法附加链接 我可以附加我试图填充的框的HTLM代码和我试图单击的按钮 文本框 <input name="ctl00$m$g_8fbfd2b6_3945_4bee_9a3b_20cfc2c846af$ctl00$grdLineItems$ctl02$txtDescription" type="text" id="ctl
<input name="ctl00$m$g_8fbfd2b6_3945_4bee_9a3b_20cfc2c846af$ctl00$grdLineItems$ctl02$txtDescription"
type="text"
id="ctl00_m_g_8fbfd2b6_3945_4bee_9a3b_20cfc2c846af_ctl00_grdLineItems_ctl02_txtDescription"
class="txtDescription"
onkeydown="return (event.keyCode!=13);"
style="width:680px;margin-top:2px;margin-bottom:2px" />
尝试-将
Elements
更改为Element
,并使用.getElementById
进行单击
IE.document.getElementById("ctl00_m_g_8fbfd2b6_3945_4bee_9a3b_20cfc2c846af_ctl00_grdLineItems_ctl02_txtDescription").Value = Range("A2")
IE.document.getElementById("ctl00_m_g_8fbfd2b6_3945_4bee_9a3b_20cfc2c846af_ctl00_ButtonAdd").Click
在现有答案中,您已经获得了最佳选择器方法,即使用元素id属性 下面是使用页面上的CSS和id选择器(“
”)实现相同功能的替代语法
欢迎使用Stack Overflow-请确保以正确的格式添加代码,以便我们能够轻松查看-下次,只需从模块中复制并粘贴代码,突出显示整个块并按Ctrl+K。语法为
。getElementById
,不是getElementsById
。简洁明了。+@QHarr现在我会使用querySelector,但我只是想修复他在当前语法中的小错误:)Lol。你的答案更适合解释错误,你使用了最快的选择器。很高兴听到对querySelector的爱!呜呜呜。@QHarr哦,是的。。。querySelector(还有你)在我即将结束的这个可怕的程序中为我节省了无数的时间(万岁!)。恭喜进步!您好,谢谢您的指点。我尝试了查询选择器,获得了与对象“IWEBBrowser2”相同的错误方法“Document”。尝试将ie声明为新的InternetExplorerMediumDiumDiumDiumDiumDiumDiumDiumDiumDiumDiumDiumDiumDiumDiumDiumDiumDiumDiumDiumDiumDiumDiumDiumDiumDiumDiumDiumDiumDiumDiumDiumDiumDiumDiumDiumDiumDiumDium。在保存并写入之前,该值不存在。保存之前没有任何值。这是常见的吗?
Sub Fill_in_eShipper()
Dim IE As Object Dim web_address As String
Dim number_of_elements As Integer
Dim MydataSet As Variant
Dim ObjCollection As Object
Windows("eshipper filler.xlsm").Activate
Set IE = CreateObject("InternetExplorer.Application")
web_address = Range("c2") Range("D2").Formula = "=ROWS(A1:A161)-1"
number_of_elements = Range("d2")
IE.navigate web_address
IE.Visible = True
While IE.Busy DoEvents 'Wait till IE is done loading Wend
'Number of elements to populate
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range("D1").Select
ActiveCell.FormulaR1C1 = "=ROWS(RC[-3]:R[160]C[-3])-1"
IE.document.getelementsbyid("ctl00_m_g_8fbfd2b6_3945_4bee_9a3b_20cfc2c846af_ctl00_grdLineItems_ctl02_txtDescription").Value = Range("A2")
IE.document.All("ctl00$m$g_8fbfd2b6_3945_4bee_9a3b_20cfc2c846af$ctl00$ButtonAdd").Click
End Sub
IE.document.getElementById("ctl00_m_g_8fbfd2b6_3945_4bee_9a3b_20cfc2c846af_ctl00_grdLineItems_ctl02_txtDescription").Value = Range("A2")
IE.document.getElementById("ctl00_m_g_8fbfd2b6_3945_4bee_9a3b_20cfc2c846af_ctl00_ButtonAdd").Click
With ie.document
.querySelector("#ctl00_m_g_8fbfd2b6_3945_4bee_9a3b_20cfc2c846af_ctl00_grdLineItems_ctl02_txtDescription").Value = Range("A2")
.querySelector("#ctl00_m_g_8fbfd2b6_3945_4bee_9a3b_20cfc2c846af_ctl00_ButtonAdd").Click
End With