【发布时间】:2018-10-10 15:15:06
【问题描述】:
我正在尝试自动填充重复且耗时的 IE 站点。
我编写了启动 IE 的代码,使其对用户可见,并调整需要填写的变量。
我找到了文本框的ID和名称,但我无法填写。
该网站是公司网站,所以我无法附上链接。
我可以附上我要填充的框和我要单击的按钮的 HTLM 代码。
文本框
<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" />
按钮点击
<input type="submit"
name="ctl00$m$g_8fbfd2b6_3945_4bee_9a3b_20cfc2c846af$ctl00$ButtonAdd"
value="Add New Item"
id="ctl00_m_g_8fbfd2b6_3945_4bee_9a3b_20cfc2c846af_ctl00_ButtonAdd"
style="color:Black;background-color:White;font-size:Medium;font-weight:bold;width:150px;"/>
我尝试了不同的方法。我尝试按名称获取文本框,但在文档命令中出现错误。
我的代码
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
【问题讨论】:
-
欢迎使用 Stack Overflow - 请确保以正确的格式添加您的代码,以便我们能够轻松查看 - 下次,只需从您的模块中复制并粘贴代码,突出显示整个块,然后按 Ctrl + K。
-
语法是
.getElementById,而不是getElementsById。
标签: excel vba internet-explorer web-scraping