IE11 Automation via Excel VBA - company webpage

↘锁芯ラ 提交于 2019-12-02 05:47:19

问题


First I'm very new to trying to automate IE via Excel VBA. That being said, I'm working to automate a login to a company-specific webpage (only accessible to our employees). The goal is to automate the login (employee number, password and click Login). I find Firefox to be particularly helpful in identifying fields so that's what I'm using in the screenshot.

I found some code online to navigate to a webpage and enter something into a search box. I've modified that as follows (the included link is not real).

Finally to the issue. If I enter a webpage like www.google.com for example, all will execute fine. But when I change to my company link, the code freezes at the Do While and I get the error shown. So my question is why it works for a general webpage but not for my company specific one? If I comment-out that line, I still get the disconnected error when debugging. Assuming that issue is an easy one to resolve, have I also properly identified the field?

Hopefully I've included enough info for you. If not, please let me know what else may be required. Thanks in advance for your help!

Error

'start a new subroutine called SearchBot
Sub SearchBot()

'dimension (declare or set aside memory for) our variables
Dim objIE As InternetExplorer 'special object variable representing the IE browser

'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer

'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True

'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate "http://sampletext.asp"

'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

'enter value in the employee number box
objIE.document.getElementByName("txtEmployeeNum").Value = "123456"

Employee Number


回答1:


Correct name of the method is getElementsByName.

You also want to operate on element of collection returned by this method, not whole collection. Using (0) index will allow to work on 1st element of collection.

Change:

objIE.document.getElementByName("txtEmployeeNum").Value = "123456"

to:

objIE.document.getElementsByName("txtEmployeeNum")(0).Value = "123456"

With such corrected code, you should step through code with F8 in VB Editor. For example by hovering over, see if objIE.Busy ever gets FALSE and especially if objIE.readyState ever reaches 4 - if only 3, try objIE.readyState < 3 instead.

EDIT:

Try replacing:

Dim objIE As InternetExplorer
Set objIE = New InternetExplorer

with:

Dim objIE As Object
Set objIE = GetObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}")

You may also need to change objIE.Navigate with objIE.Navigate2



来源:https://stackoverflow.com/questions/50499885/ie11-automation-via-excel-vba-company-webpage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!