Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.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 在登录页面的div中输入数据_Excel_Vba - Fatal编程技术网

Excel 在登录页面的div中输入数据

Excel 在登录页面的div中输入数据,excel,vba,Excel,Vba,我正准备登录,但看起来像是。。。没什么好的。有趣的代码部分和错误的屏幕附在顶部。我希望您能够提供帮助。您希望通过css类名获取第一个输入字段“wpfe登录输入完整wpfe本机褪色输入ng未触及ng原始ng有效”。我写名字是因为每个用空格分隔的字符串都是自己的css类名。加载页面时,最后一个css类名无效。它是无效的。这就是你犯错误的原因 解决方案是只使用第一个css类wpfe登录输入full。但这还不足以实现你的目标 加载第一个html代码后,IE报告浏览器不再忙,IE就交给您了;-)之后将加载


我正准备登录,但看起来像是。。。没什么好的。有趣的代码部分和错误的屏幕附在顶部。我希望您能够提供帮助。

您希望通过css类名获取第一个输入字段
“wpfe登录输入完整wpfe本机褪色输入ng未触及ng原始ng有效”
。我写名字是因为每个用空格分隔的字符串都是自己的css类名。加载页面时,最后一个css类名无效。它是无效的。这就是你犯错误的原因

解决方案是只使用第一个css类
wpfe登录输入full
。但这还不足以实现你的目标

加载第一个html代码后,IE报告浏览器不再忙,IE就交给您了;-)之后将加载动态内容。所以你必须等到这一切结束

但即使这样,登录仍然不起作用。每个输入字段必须触发两个html事件

此代码有效:

Sub Login()

  Const url As String = "https://mdemo.cqg.com/cqg/desktop/logon"
  
  Dim ie As Object
  Dim HTMLDoc As Object
  Dim nodeUsername As Object
  Dim nodePassword As Object
  
  Set ie = CreateObject("InternetExplorer.Application")
  ie.Visible = True
  ie.navigate url
  Do While ie.Busy = True: DoEvents: Loop
  Application.Wait (Now + TimeSerial(0, 0, 5))
  
  Set HTMLDoc = ie.document
  
  Set nodeUsername = HTMLDoc.getElementsByClassName("wpfe-logon-input-full")(0)
  Call TriggerEvent(HTMLDoc, nodeUsername, "compositionstart")
  nodeUsername.Value = "TestName"
  Call TriggerEvent(HTMLDoc, nodeUsername, "compositionend")
  
  Set nodePassword = HTMLDoc.getElementsByClassName("wpfe-logon-input-full")(1)
  Call TriggerEvent(HTMLDoc, nodePassword, "compositionstart")
  nodePassword.Value = "TestPassword"
  Call TriggerEvent(HTMLDoc, nodePassword, "compositionend")
  
  HTMLDoc.getElementByID("login").Click
End Sub
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
使用此过程触发html事件:

Sub Login()

  Const url As String = "https://mdemo.cqg.com/cqg/desktop/logon"
  
  Dim ie As Object
  Dim HTMLDoc As Object
  Dim nodeUsername As Object
  Dim nodePassword As Object
  
  Set ie = CreateObject("InternetExplorer.Application")
  ie.Visible = True
  ie.navigate url
  Do While ie.Busy = True: DoEvents: Loop
  Application.Wait (Now + TimeSerial(0, 0, 5))
  
  Set HTMLDoc = ie.document
  
  Set nodeUsername = HTMLDoc.getElementsByClassName("wpfe-logon-input-full")(0)
  Call TriggerEvent(HTMLDoc, nodeUsername, "compositionstart")
  nodeUsername.Value = "TestName"
  Call TriggerEvent(HTMLDoc, nodeUsername, "compositionend")
  
  Set nodePassword = HTMLDoc.getElementsByClassName("wpfe-logon-input-full")(1)
  Call TriggerEvent(HTMLDoc, nodePassword, "compositionstart")
  nodePassword.Value = "TestPassword"
  Call TriggerEvent(HTMLDoc, nodePassword, "compositionend")
  
  HTMLDoc.getElementByID("login").Click
End Sub
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

如果用F8执行,会怎么样?出现错误的是哪一行?我想你可能是指html类名而不是css。上面是一个多值类。类是一个快速css选择器方法,实际上,您可以使用HTMLDoc.querySelector(“.wpfe logon input full”)和HTMLDoc.querySelector(“.wpfe logon input full+.wpfe logon input full”)进行单节点匹配,而不是索引到集合中;不过,在本例中,我可能只会在选择器中使用name属性,因为读入代码既美观又干净,而且得益于单节点匹配,例如HTMLDoc.querySelector(“[name=loginPassword]”)。OFC,您可能希望不要冒险远离原始代码。我还担心在事件发生后使用HTMLDoc变量;解决问题。document@QHarr关于css和html类名之间的区别,您是对的。我对这些条款太含糊了。感谢您对querySelector()的解释以及您对其在本例中使用的想法。在我如此活跃之前,我从未听说过querySelector()。我想我甚至是第一次在你的一篇帖子上读到这件事。但我必须承认,我没有和它合作过很多。但希望您发人深省的意见能帮助我在将来更多地使用它。这真的为使用vba进行web抓取打开了大门。没有非VBA库那么多,但肯定足够做一些有趣的事情。很高兴看到你帮助人们解决了许多网络抓取问题:-)非常感谢。现在我有另一个问题。如果你能帮助我,我将非常感激。