IE单击没有关联链接的按钮(使用Excel VBA)
我想点击网页上的一个“按钮”,但我的问题是这个按钮似乎没有链接。顺便说一下,我的措辞你可以看到我对网络浏览器的语言一点也不熟悉。 不管怎样,我最常用的是internet explorer,下面是我到目前为止的代码IE单击没有关联链接的按钮(使用Excel VBA),excel,vba,internet-explorer,Excel,Vba,Internet Explorer,我想点击网页上的一个“按钮”,但我的问题是这个按钮似乎没有链接。顺便说一下,我的措辞你可以看到我对网络浏览器的语言一点也不熟悉。 不管怎样,我最常用的是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 = C
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
为了清晰起见,下面是我试图与之交互的“按钮”周围的网页代码。
我做了很多研究,但我现在已经走到了死胡同。任何帮助都将不胜感激。提前发送 解决了最终代码:在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
返回具有所有给定类名的所有子元素的类似数组的对象,因此我认为它应该是IE.document.getElementsByClassName(“rc内容按钮”)(0)。如果它是具有类名的第一个元素,请单击
您也可以尝试:IE.document.querySelector(“div.rc-content-buttons-input”)。单击
我认为
IE.document.getElementById(“B91938241817236808”)。单击也是正确的。getElementsByCassName
返回一个类似数组的对象,它包含所有子元素,这些子元素都具有所有给定的类名,因此我认为它应该是IE.document.getElementsByCassName(“rc内容按钮”)(0)。如果它是具有类名的第一个元素,请单击
您也可以尝试:IE.document.querySelector(“div.rc-content-buttons-input”)。单击
我认为IE.document.getElementById(“B91938241817236808”)。单击也是正确的。它位于输入中,而不是标记元素,因此收集标记不会捕获您想要的内容。正如注释中所建议的那样,使用id是一种方法。如果找不到元素,请检查元素是否位于需要协商的父帧或iframe中。一般语法是
ie.document.getElementsByTagName("frame")(appropriateIndexHere).contentDocument.getElementById("B91938241817236808")
ie.document.getElementsByTagName("iframe")(appropriateIndexHere).contentDocument.getElementById("B91938241817236808")
如果id是动态的,则可以使用值
属性
ie.document.querySelector("[value='Créer']")
ie.document.getElementsByTagName("frame")(appropriateIndexHere).contentDocument.querySelector("[value='Créer']") 'etc
由于有活动,您可能需要启动它
ie.document.querySelector("[value='Créer']").FireEvent "onclick"
ie.document.getElementsByTagName("frame")(appropriateIndexHere).contentDocument.querySelector("[value='Créer']").FireEvent "onclick" 'etc
并使用适当的页面加载等待。所以这个,
Do While IE.Busy 'loading page
Application.Wait DateAdd("s", 1, Now)
Loop
应该是
While ie.Busy Or ie.ReadyState <> 4: DoEvents:Wend
在ie.Busy或ie.ReadyState 4:DoEvents:Wend时
它位于输入中,而不是标记元素,因此收集标记不会捕获您想要的内容。正如注释中所建议的那样,使用id是一种方法。如果找不到元素,请检查元素是否位于需要协商的父帧或iframe中。一般语法是
ie.document.getElementsByTagName("frame")(appropriateIndexHere).contentDocument.getElementById("B91938241817236808")
ie.document.getElementsByTagName("iframe")(appropriateIndexHere).contentDocument.getElementById("B91938241817236808")
如果id是动态的,则可以使用值
属性
ie.document.querySelector("[value='Créer']")
ie.document.getElementsByTagName("frame")(appropriateIndexHere).contentDocument.querySelector("[value='Créer']") 'etc
由于有活动,您可能需要启动它
ie.document.querySelector("[value='Créer']").FireEvent "onclick"
ie.document.getElementsByTagName("frame")(appropriateIndexHere).contentDocument.querySelector("[value='Créer']").FireEvent "onclick" 'etc
并使用适当的页面加载等待。所以这个,
Do While IE.Busy 'loading page
Application.Wait DateAdd("s", 1, Now)
Loop
应该是
While ie.Busy Or ie.ReadyState <> 4: DoEvents:Wend
在ie.Busy或ie.ReadyState 4:DoEvents:Wend时
目标元素似乎没有内部文本。但是它确实有一个ID。所以请尝试IE.document.getElementById(“B919…”)。单击。Hello@Domenic,tx以获取建议。我相信我已经试过了,但没有成功。我明天早上第一件事就是试试,然后告诉你。同时,如果其他人有其他建议,我也会在12小时内尝试。请解释不成功意味着什么。您在尝试Domenic的建议时出错了吗?@Domenic您的解决方案非常有效!!我以为我试过了,但似乎没有。谢谢你的帮助。不客气,干杯!看起来目标元素没有内部文本。但是它确实有一个ID。所以请尝试IE.document.getElementById(“B919…”)。单击。Hello@Domenic,tx以获取建议。我相信我已经试过了,但没有成功。我明天早上第一件事就是试试,然后告诉你。同时,如果其他人有其他建议,我也会在12小时内尝试。请解释不成功意味着什么。您在尝试Domenic的建议时出错了吗?@Domenic您的解决方案非常有效!!我以为我试过了,但似乎没有。谢谢你的帮助。不客气,干杯!谢谢你的提醒。我将修改我的答案。Tx Yu Zhou您的第二个建议(querySelector(…)。单击另一个工作感谢您的提醒。我将修改我的答案。Tx Yu Zhou您的第二个建议(querySelector(…)。单击还可以工作非常感谢您的QHarr!确实ID方法可以工作,但是querySelector更优雅,因为在读取“value='Créer'然后读取“B91…”时,代码更容易解释。它不是在一个框架中,但它是一个事件,所以我必须使用。FireEvent。我还修改了我的加载页等待循环。在那里获得了一些秒。非常感谢。我应该早点发布此消息,而不是自己搜索4个小时。我这里有一个相关问题,任何人(@QHarr可能)能帮上忙吗?非常感谢你的QHarr!的确ID方法是有效的,但是querySelector更优雅,因为在阅读“value='Créer'然后阅读“B91…”时,代码更容易解释。它不是在一个框架中,但它是一个事件,所以我必须使用。FireEvent。我还修改了我的加载页等待循环。在那里获得了一些秒。非常感谢。我应该早点发布此消息,而不是自己搜索4个小时。我这里有一个相关问题,有人(@QHarr)可以帮助吗?