Html 用excel VBA“点击”一个网站的按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27549647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'click' a website's button with excel VBA
提问by kamelkid2
I have a script I am writing where I can execute a form on a website through a macro. I am able to open up internet explorer and pass all the variables correctly however when it comes time to submit, I am a bit lost.
我有一个正在编写的脚本,我可以在其中通过宏在网站上执行表单。我能够打开 Internet Explorer 并正确传递所有变量,但是当需要提交时,我有点迷茫。
this is the element on the website i want to click - it is a button titled "buy"
这是我想点击的网站上的元素 - 它是一个标题为“购买”的按钮
<input type="submit" name="submit" value="Buy">
I have two problems:
我有两个问题:
1) i don't know how to properly reference this within vba 2) there is a button right next to it that will perform a sell (the exact opposite of what i want to do) and the element for that is:
1)我不知道如何在 vba 中正确引用它 2)它旁边有一个按钮可以执行卖出(与我想要做的完全相反),其元素是:
<input type="submit" name="submit" value="Sell">
Does anyone know appropriate code to hit the 'buy' button?
有谁知道点击“购买”按钮的适当代码?
here is my code thus far:
到目前为止,这是我的代码:
Dim IE As Object
Set IE = New InternetExplorer
IE.Visible = True
IE.Navigate "somewebsite.com"
Do While IE.Busy: DoEvents: Loop
Do While IE.ReadyState <> 4: DoEvents: Loop
IE.Document.All("resourceoption").Value = "item"
IE.Document.All("amount").Value = 1
IE.Document.All("priceper").Value = 99
Do While IE.Busy: DoEvents: Loop
Do While IE.ReadyState <> 4: DoEvents: Loop
采纳答案by kamelkid2
With IE.document
Set elems = .getElementsByTagName("input")
For Each e In elems
If (e.getAttribute("value") = "Buy") Then
e.Click
Exit For
End If
Next e
End With
the above snippet performs the task needed
上面的代码片段执行了所需的任务