Javascript link doesn't respond in selenium excel vba

Viewed 612

I am trying to do such a line that clicks on a javascript link

.FindElementById("ctl00_ContentPlaceHolder1_LinkButton4").WaitDisplayed(True, 3000).Click

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

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

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'"

enter image description here

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? enter image description here

1 Answers

Try the following at the top

 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

Then

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','');"

Clear cache and navigate back (as button is in shadow-root you need to use /deep/ combinator)

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);"

Or potentially,

d.GoBack
Related