HTML& VBA Visual Basic自动输入网站

时间:2018-03-08 15:43:10

标签: html vba excel-vba excel

我的公司有一个"链接"我可以去... http://blablabla ...在哪里有一个盒子,我可以输入一个8位数字(在它旁边的方框中显示批号)和一个按钮,我可以点击查看报告。就是这样。这就是页面上的所有内容。 (输入框和视图报告按钮)

我在单元格范围B3中的excel中有一个数字列表:例如B7(10378851,10378857,10488213等)

我想打开excel并运行一个自动进入网站的代码,自动输入第一个数字,自动"点击"视图报告按钮。

然后会出现一个"导出数据按钮" - 我想自动点击哪个有下拉列表(csv,txt,excel等)我想自动选择" excel"

我发现了一个网站,解释了如何做类似的事情,但我被卡住了。

Sub GetTable()

Dim ieApp As InternetExplorer
Dim ieDoc As Object
Dim ieTable As Object
Dim clip As DataObject

'create a new instance of ie
Set ieApp = New InternetExplorer

'you don’t need this, but it’s good for debugging
ieApp.Visible = True

'assume we’re not logged in and just go directly to the login page
ieApp.Navigate "http://severe-frost-552.heroku.com/login"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

Set ieDoc = ieApp.Document

'fill in the login form – View Source from your browser to get the control names
With ieDoc.forms(0)
.login.Value = "dailydose"
.Password.Value = "password"
.submit
End With
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

'now that we’re in, the rest of this code isn't relevant because it copied a table of data...

这是网站......

http://dailydoseofexcel.com/archives/2011/03/08/get-data-from-website-that-requires-a-login/

如您所见,源代码说:

<form action="/session" method="post"><div style="margin:0;padding:0;display:in...
<input id="login" name="login" type="text" /></p>

我认为这是他们获得这部分代码的地方:

With ieDoc.forms(0)
.login.Value = "dailydose"

查看我的网站源代码,看起来我有:

<DIV id=ReportViewerControl_ctl04_ctl03><INPUT id=ReportViewerControl_ctl04_ctl03_txtValue class=null style= size=30 name=ReportViewerControl$ctl04$ctl03$txtValue> </DIV>
<INPUT id=ReportViewerControl_ctl04_ctl03_txtValue class=null style= size=30 name=ReportViewerControl$ctl04$ctl03$txtValue>

我认为这是代码的正确区域,因为当我键入&#34; hello&#34;它会显示在输入框中,如下所示:

<INPUT id=ReportViewerControl_ctl04_ctl03_txtValue class=null style= size=30 value=Hello name=ReportViewerControl$ctl04$ctl03$txtValue>

这与简单示例有很大不同......我不知道如何在我的代码中编写此输入...

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

ieDoc.ReportViewerControl_ctl04_ctl03_txt.Value = "Hello"

如果这不起作用:

ieDoc.getElementById("ReportViewerControl_ctl04_ctl03_txt").Value = "Hello"

免责声明:我没有测试过代码

提示:你是什么&#34;玩&#34;被称为&#34; web-scraping&#34;。你可以找到合适的例子here