[英]IE click on a button that has no link associated to it (using Excel VBA)
I want to click on a "button" on a web page, but my problem is that this button doesn't seem to have link attach to it. 我想单击网页上的“按钮”,但是我的问题是此按钮似乎没有链接。 By the way I'm phrasing this you can see I'm not familiar at all with the language of web browser. 顺便说一下,您可以看到我对Web浏览器的语言完全不熟悉。 Anyway I most use internet explorer and here's the code I have so far 无论如何,我最常使用Internet Explorer,这是到目前为止的代码
Sub click_button_no_hlink()
Dim i As Long
Dim IE As Object
Dim Doc As Object
Dim objElement As Object
Dim objCollection As Object
Set IE = CreateObject("InternetExplorer.Application") 'create IE instance
IE.Visible = True
IE.Navigate "https://apex.xyz.qc.ca/apex/prd1/f?p=135:LOGIN_DESKTOP::::::" ' Adress of web page
Do While IE.Busy 'loading page
Application.Wait DateAdd("s", 1, Now)
Loop
'-------------Usually I would do something like that and it would works well.-----------------
Set link = IE.document.getElementsByTagName("a")
For Each l In link
a = l.innerText
If l.innerText = "Créer" Then '
l.Click
Exit For
End If
Next
'-------------That method works fine on other type of hlink to the page by the way------------
'-----------------------I also tried various methods around this with no luck------------------------------
Set objCollection = IE.document.getElementsByClassName("rc-content-buttons") 'recherche du bouton "Créer"
'---------------------------------
'--------------------or this also doesn't work-------------------------------
For Each btn In IE.document.getElementsByClassName("rc-content-buttons")
If btn.getAttribute("id") = "B91938241817236808" Then
btn.Click
Exit For
End If
Next btn
End Sub
For sake of clarity here's the code of the web page around the "button" I'm trying to interact with. 为了清楚起见,以下是我尝试与之交互的“按钮”周围的网页代码。
I've made many research but I'm in a dead end right now. 我进行了许多研究,但我现在处于死胡同。 Any help will be greatly appreciate. 任何帮助将不胜感激。
Tx in advance. 提前发送。
SOLVED FINAL CODE: With help from DOMENIC and QHARR and Yu Zhou 解决的最终代码:在DOMENIC,QHARR和Yu Zhou的帮助下
Sub click_button_no_hlink()
Dim i As Long
Dim IE As Object
Dim Doc As Object
Dim objElement As Object
Dim objCollection As Object
Set IE = CreateObject("InternetExplorer.Application") 'create IE instance
IE.Visible = True
IE.Navigate "https://apex.xyz.qc.ca/apex/prd1/f?p=135:LOGIN_DESKTOP::::::" ' Adress of web page
While IE.Busy: DoEvents: Wend 'loading page
IE.document.querySelector("[value='Créer']").FireEvent "onclick" 'works like a charm
' IE.document.querySelector("div.rc-content-buttons input").Click
'also works but speaks less by itself when reading code
' IE.document.getElementById("B91938241817236808").Click
'also works
End Sub
getElementsByClassName
returns an array-like object of all child elements which have all of the given class names, so I think it should be IE.document.getElementsByClassName("rc-content-buttons")(0).Click
if it is the first element with the classname. getElementsByClassName
返回具有所有给定类名的所有子元素的类似数组的对象,因此我认为它应为IE.document.getElementsByClassName("rc-content-buttons")(0).Click
是否为第一个具有类名的元素。
You could also try: IE.document.querySelector("div.rc-content-buttons input").Click
. 您也可以尝试: IE.document.querySelector("div.rc-content-buttons input").Click
。
And I think IE.document.getElementById("B91938241817236808").Click
is also right. 而且我认为IE.document.getElementById("B91938241817236808").Click
也是正确的。
It is within an input not a tag element so gathering a tags will not capture what you want. 它在输入中,而不是标签元素,因此收集标签不会捕获您想要的内容。 Using the id, as suggested in comments is one way to go. 按照注释中的建议使用id是一种方法。 If you get element not found then check whether your element is within a parent frame or iframe which needs to be negotiated. 如果未找到元素,请检查您的元素是在父框架还是需要协商的iframe中。 General syntax for that is 通用语法是
ie.document.getElementsByTagName("frame")(appropriateIndexHere).contentDocument.getElementById("B91938241817236808")
ie.document.getElementsByTagName("iframe")(appropriateIndexHere).contentDocument.getElementById("B91938241817236808")
If the id is dynamic you can use the value
attribute 如果id是动态的,则可以使用value
属性
ie.document.querySelector("[value='Créer']")
ie.document.getElementsByTagName("frame")(appropriateIndexHere).contentDocument.querySelector("[value='Créer']") 'etc
As there is an event, you may need to fire it. 由于发生事件,您可能需要触发它。
ie.document.querySelector("[value='Créer']").FireEvent "onclick"
ie.document.getElementsByTagName("frame")(appropriateIndexHere).contentDocument.querySelector("[value='Créer']").FireEvent "onclick" 'etc
And use a proper page load wait. 并使用适当的页面加载等待。 So this, 所以这,
Do While IE.Busy 'loading page
Application.Wait DateAdd("s", 1, Now)
Loop
Should be 应该
While ie.Busy Or ie.ReadyState <> 4: DoEvents:Wend
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.