Excel VBA登录IE站点 [英] Excel VBA Login to IE Site
问题描述
我是VBA的新手,也是Stack Overflow的新手,尽管我已经阅读了一段时间了.我已经搜索了很多有关编码问题的答案,但似乎找不到答案.我试图登录到网站,导航到页面,然后从该页面抓取数据.我已经开始构建代码,这些代码是从互联网上找到的代码一起解析的.我似乎无法让IE在使用获取元素"查找其ID之后,甚至将我的凭据放在适当的用户和密码框中.我最终遇到的是运行时错误424-必需的对象.预先感谢您的帮助.
I am a VBA novice and new to Stack Overflow, even though I have been reading posts on here for a while. I have searched a lot for an answer to my coding problem, but I can't seem to find the answer. I am trying to login to a website, navigate to a page, and then scrape data from that page. I have started to build the code which I have parsed together from what I've found on the internet. I can't seem to get IE to even put my credentials in the appropriate user and PW boxes after looking up their IDs using "get elements". What I end up with is Run Time Error 424 - Object Required. Thanks in advance for any help.
'Open IE, navigate to the desired page and loop until fully loaded
Set IE = CreateObject("InternetExplorer.Application")
site = "https://www.ivolatility.com/login.j"
With IE
.Visible = True
.navigate site
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
End With
'Input the userid and password
IE.Document.getElementById("username").Value = "xxxxxx"
IE.Document.getElementById("password").Value = "xxxxxx"
'Click the "Search" button
IE.Document.getElementById("login_go").Click
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
推荐答案
您试图将HTML输入元素的 name 用作其 id ,但这两个不一定是相同,尽管有时您会看到.
You are attempting to use the HTML input element's name as its id The two are not necessarily the same although sometimes you will see.
<input type="text" id="username" name="username" value="" size="10" class="s2" maxlength="63 ">
但这不是他的情况.您需要按理论名称而不是id对待元素.
But that is not he case here. You need to treat the elements by theor names and not ids.
dim n as long 'this belongs at hte top witrh the other Dims
'Input the userid and password
For n = 0 To ie.document.getelementsbytagname("input").Length - 1
With ie.document.getelementsbytagname("input")(n)
Select Case LCase(.Name)
Case "username"
.Value = "xxxxxx"
Case "password"
.Value = "zzzzzz"
Case "login_go"
.Click
n = 99999
Case Else
'ignore
End Select
End With
Next n
Do While ie.Busy Or ie.readyState <> 4: DoEvents: Loop
在这种情况下,单击有效,因为提交位于名为 username 和 password 的输入元素之后.
The click works in this case because the submit comes after the input elements named username and password.
这篇关于Excel VBA登录IE站点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!