Javascript链接在Selenium Excel VBA中无响应 [英] Javascript link doesn't respond in selenium excel vba
问题描述
我正在尝试点击JavaScript链接这样的行
I am trying to do such a line that clicks on a javascript link
.FindElementById("ctl00_ContentPlaceHolder1_LinkButton4").WaitDisplayed(True, 3000).Click
此行没有任何错误,但有时它不起作用..我说有时不总是..有时是工作,有时是停止,我不知道原因我必须重新启动代码才能启动新的bot(驱动程序),在某些情况下,我必须重新启动很多次,直到此链接正常工作
There's no any errors at this line but sometimes it doesn't work .. I say sometimes not always.. sometimes work and sometimes stopped and I don't know the reason I have to restart the code to start new bot (driver) and at some cases I have to restart so many times till this link worked
这是此javascript链接的html部分
Here's the html part of this javascript link
<a id="ctl00_ContentPlaceHolder1_LinkButton4" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LinkButton4','')" style="display:inline-block;width:280px;text-decoration:none;height:22px;font-weight:bold;font-size:11pt;color:Maroon;border-width:1px;border-style:Dashed;border-color:#404040;background-color:#FFFFC0;vertical-align: middle; text-align: center; cursor: hand;">Some Text Removed For Privacy</a>
如何使此链接正常工作?或者有没有变通的方法来使其正常工作..?我试图解决这一问题,但根本没有发现任何线索
How can I make this link work ... or is there a workaround to make it work ..? I tried to solve that point but I didn't find any clue at all
这是响应式部分的html部分(我看不出任何区别)
This is the html part for a responsive one (I don't see any difference)
<a id="ctl00_ContentPlaceHolder1_LinkButton4" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$LinkButton4','')" style="display:inline-block;width:280px;text-decoration:none;height:22px;font-weight:bold;font-size:11pt;color:Maroon;border-width:1px;border-style:Dashed;border-color:#404040;background-color:#FFFFC0;vertical-align: middle; text-align: center; cursor: hand;">Some Text Removed For Privacy/a>
这是执行此行后的错误快照
Here's a snapshot of the error after executing this line
.ExecuteScript "javascript:__doPostBack('ctl00$ContentPlaceHolder1$LinkButton4'"
这是网络快照(可能有用)>>我看到302(我认为应该有更新的缓存..),有没有办法清除漫游器的缓存?
This is Network snapshot (may be useful) >> I see 302 (which I think there should be newer cache ..) so is there a way to clear the cache of the bot?
推荐答案
在顶部尝试以下操作
Const JS_WAIT_CLICKABLE = _
"var target = this, endtime = Date.now() + arguments[0];" & _
"(function check_clickable() {" & _
" var r = target.getBoundingClientRect(), x = r.left+r.width/2, y = r.top+r.height/2;" & _
" for (var e = document.elementFromPoint(x , y); e; e = e.parentElement)" & _
" if (e === target){ callback(target); return; }" & _
" if (Date.now() > endtime) { callback(target); return; }" & _
" setTimeout(check_clickable, 60);" & _
"})();" 'by @florentbr
然后
With .FindElementById("ctl00_ContentPlaceHolder1_LinkButton4")
.ExecuteAsyncScript(JS_WAIT_CLICKABLE, 10000) _
.Click
End With
您也可以尝试直接执行
You could also try to execute direct
.ExecuteScript "javascript:__doPostBack('ctl00$ContentPlaceHolder1$LinkButton4','');"
清除缓存并向后导航(由于按钮位于阴影根目录中,因此您需要使用/deep/
组合器)
d.get "chrome://settings/clearBrowserData"
Dim button As Object
d.FindElementByCss("* /deep/ #clearBrowsingDataConfirm").Click
Set button = d.FindElementByCss("* /deep/ #clearBrowsingDataConfirm")
Const MAX_WAIT_SEC As Long = 15
Dim t As Date
t = Timer
Do
DoEvents
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop Until button Is Nothing
d.ExecuteScript "window.history.go(-2);"
或可能
d.GoBack
这篇关于Javascript链接在Selenium Excel VBA中无响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!