通过Excel VBA进行IE11自动化-公司网页 [英] IE11 Automation via Excel VBA - company webpage

查看:228
本文介绍了通过Excel VBA进行IE11自动化-公司网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我对尝试通过Excel VBA自动化IE非常陌生. 话虽如此,我正在努力使登录公司的网页自动化(仅我们的员工可以访问).目标是使登录自动进行(员工编号,密码并单击登录").我发现Firefox在识别字段方面特别有用,这就是我在屏幕快照中使用的内容.

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).

最后是问题.例如,如果我输入类似www.google.com的网页,则一切正常.但是,当我转到公司链接时,代码在"Do While"时冻结,并且显示错误.所以我的问题是为什么它适用于一般网页而不适用于我公司的特定网页?如果我注释掉该行,则在调试时仍然会出现断开连接的错误.假设该问题很容易解决,我是否也已正确识别此领域?

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!

'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"

员工编号

推荐答案

该方法的正确名称是getElementsByName.

您还希望对此方法返回的collection元素(而不是整个collection)进行操作.使用(0)索引将允许处理集合的第一个元素.

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.

更改:

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

收件人:

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

使用这种经过更正的代码,您应该在VB编辑器中使用F8逐步执行代码.例如,将鼠标悬停在上面,查看objIE.Busy是否获得FALSE,尤其是objIE.readyState是否达到4-如果只有3,则尝试使用objIE.readyState < 3.

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.

尝试更换:

Dim objIE As InternetExplorer
Set objIE = New InternetExplorer

具有:

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

您可能还需要用objIE.Navigate2

这篇关于通过Excel VBA进行IE11自动化-公司网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆