Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
单击网页按钮功能在excel宏中不起作用_Excel_Vba - Fatal编程技术网

单击网页按钮功能在excel宏中不起作用

单击网页按钮功能在excel宏中不起作用,excel,vba,Excel,Vba,我正在尝试创建一个联邦快递跟踪自动化系统,但我无法点击跟踪按钮。有什么想法吗 Sub Fedex() Dim IE As Object Dim doc As HTMLDocument Set IE = New InternetExplorerMedium IE.Visible = True IE.navigate "https://www.fedex.com/en-us/home.html" Do

我正在尝试创建一个联邦快递跟踪自动化系统,但我无法点击跟踪按钮。有什么想法吗

Sub Fedex()
    
    Dim IE As Object
    Dim doc As HTMLDocument
    
    Set IE = New InternetExplorerMedium
    IE.Visible = True
    IE.navigate "https://www.fedex.com/en-us/home.html"
    
    Do While IE.Busy Or IE.READYSTATE = 4
    Loop
    
    Application.Wait (Now + TimeValue("0:00:07"))
    
    Set searchbx = IE.document.getElementsByName("trackingnumber")(0)
    searchbx.Value = "Howdy!"

    IE.document.getElementById("btnSingleTrack").Click

End Sub
编辑-通过url参数直接执行 您可以操纵导航的url。看看下面的例子。将最后一个参数的值
YourTrackingNumbers
更改为工作跟踪编号,并手动测试:

https://www.fedex.com/apps/fedextrack/?action=track&cntry_code=us&locale=en_US&trackingnumber=YourTrackingNumberHere

第一个答案是让它以你的方式工作 搜索框包含事件。您必须在设置跟踪编号后触发更改事件。我以前从未见过它,但在触发更改事件后,您必须等待几秒钟才能使按钮单击工作

Sub Fedex()
  Dim ie As Object
  Dim searchBox As Object
  
  'Initialize Internet Explorer, set visibility,
  'call URL and wait until page is fully loaded
  Set ie = CreateObject("InternetExplorer.Application")
  ie.Visible = True
  ie.navigate "https://www.fedex.com/en-us/home.html"
  Do While ie.READYSTATE <> 4: DoEvents: Loop
  
  Set searchBox = ie.document.getElementsByName("trackingnumber")(0)
  searchBox.Value = "Howdy!"
  'Trigger the change event of the input box
  Call TriggerEvent(ie.document, searchBox, "change")
  'I don't know why, but you have to wait a few seconds
  'Otherwise the button click will not work
  'In my tests, I need 5 seconds to make it work in every case
  'You can make your own tests for your environment
  Application.Wait (Now + TimeValue("0:00:05"))
  
  ie.document.getElementById("btnSingleTrack").Click
End Sub

成功了!非常感谢@Zwenn!我能再问一个问题吗?如果我像这样读出实际值,我会得到一个438错误strAdd=ie.document.getElementsByClassName(“重新设计SnapshotVC.snapshotController_date.dest”).innerText ThisWorkbook.Sheets(“Sheet1”).Range(“A2”).Value=strAdd'@Petolicious您至少忘记了
getElementsByClassName()(此处索引)
后面圆括号中的索引。您的代码是否要等到页面完全加载后才能访问它?@Zween这就是我需要的值所在的框的外观:2020年11月18日星期三下午4:30@Zween是的,代码将一直运行到代码的那一行。网页打开,输入跟踪ID并按下按钮,显示联邦快递的预计到达日期
Private Sub TriggerEvent(htmlDocument As Object, htmlElementWithEvent As Object, eventType As String)

  Dim theEvent As Object

  htmlElementWithEvent.Focus
  Set theEvent = htmlDocument.createEvent("HTMLEvents")
  theEvent.initEvent eventType, True, False
  htmlElementWithEvent.dispatchEvent theEvent
End Sub