如何用excel vba代码点击网站上没有id,名称或类的按钮?

时间:2017-08-09 05:54:15

标签: excel vba

这是按钮的html:

<div style = "position:absolute;top:2px;left:5px;"> == $0  
  <button title = "Start New Quote" style = "background-color: 
  rgb(4,61,111);   
  color:rgb(255,255,255); width:122px;height:33px;padding-  
  left:0px;padding-right:0px;font-size:12px;font-weight:bold;" onclick = 
  toggleNewBusinessMenu();   
  return false;"> == $0    
    <img style = "vertical-align: middle" src =  
      "/images/icons/create_quote.png"> == $0  
      "&nbsp;&nbspStart Quote&nbsp  
             "
 </button>  
 </div>  

这是我尝试过的一个选项,但在编码时是新的,所以不确定它应该如何使用没有名称或ID:

Sub Superior()
Dim ieApp As Object

'create a new instance of ie
Set ieApp = CreateObject("InternetExplorer.Application")
ieApp.Visible = True

ieApp.navigate "website"

Wait 1
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop

ieApp.document.all("login_email").Value = "username"
ieApp.document.all("login_password").Value = "password"
ieApp.document.all("login_button").Click

Wait 1
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop

ieApp.document.getElementsByTagName("$0").Click

End Sub

Sub Wait(Seconds)
    CreateObject("WScript.Shell").Run "%COMSPEC% /c ping 127.0.0.1 -n " _
    & Seconds + 1, 0, True
End Sub

我已经尝试了一切 - 谢谢!

1 个答案:

答案 0 :(得分:0)

...试

Dim HTMLButton As Object
'etc
'
'
ieApp.document.all("login_email").Value = "username"
ieApp.document.all("login_password").Value = "password"
ieApp.document.all("login_button").Click

Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop

Wait 5

For Each HTMLButton In ieApp.document.getElementsByTagName("button")
    If HTMLButton.getAttribute("title") = "Start New Quote" Then
        HTMLButton.Click
        Exit For
    End If
Next HTMLButton

希望这有帮助!