Selenium 2 - a href with JavaScript onClick() doesn't work

1,702 views
Skip to first unread message

John Gluck

unread,
Jul 2, 2010, 7:21:22 PM7/2/10
to Selenium Users
I searched the forums for a *clear* answer to this question but wasn't
able to find one.

I have tried in Selenium 2.04 with both the FirefoxDriver and
ChromeDriver to click a link that looks like this

<form id="logout" name="logout" method="post" action="/beta/member/
browse.seam" enctype="application/x-www-form-urlencoded">
109<input type="hidden" name="logout" value="logout" />
110
111<script type="text/javascript" language="Javascript">function
dpf(f) {var adp = f.adp;if (adp != null) {for (var i = 0;i <
adp.length;i++) {f.removeChild(adp[i]);}}};function apf(f, pvp) {var
adp = new Array();f.adp = adp;var ps = pvp.split(',');for (var i =
0,ii = 0;i < ps.length;i++,ii++) {var p =
document.createElement("input");p.type = "hidden";p.name =
ps[i];p.value = ps[i + 1];f.appendChild(p);adp[ii] = p;i +=
1;}};function jsfcljs(f, pvp, t) {apf(f, pvp);var ft = f.target;if (t)
{f.target = t;}f.submit();f.target = ft;dpf(f);};</script>
112<a href="#" onclick="if(typeof jsfcljs == 'function')
{jsfcljs(document.forms['logout'],'logout:j_id254,logout:j_id254','');}
return false">Sign Out</a><input type="hidden"
name="javax.faces.ViewState" id="javax.faces.ViewState" value="j_id5" /
>
113</form></li>

I have used the following locators
@FindBy(how = How.XPATH, using = "//a[@href='#']")
@FindBy(how = How.LINK_TEXT, using = "Sign Out")

In both cases, WebDriver clearly finds the element (because it passes)
and it apparently think the link is clicked, but I am not taken to the
expected page. Rather, I stay on the page.I'm guessing the JavaScript
doesn't automatically execute. So it appears I have to do something
to execute JavaScript, but I'm not sure exactly how to do this.

I tried to use the JavascriptExecutor, but clearly my understanding of
JavaScript is not extensive enough for me to figure this out easily

Can anyone give me a hand? Is there something I can set so just
clicking on this works? It worked in Selenium 1 (though it did give
an unexpected error)
Please help

Brian Davidson

unread,
Jul 6, 2010, 12:55:35 PM7/6/10
to seleniu...@googlegroups.com
I had a similar problem so I had to do the following

JavascriptLibrary jsl = new JavascriptLibrary();
        jsl.executeScript(driver,element.getAttribute("href"), "");

this is accomplished with the following library org.openqa.selenium.internal.seleniumemulation.JavascriptLibrary

this is in Java and you'll have to tinker with it to work in your situation but it should work I think


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.


Ian Danforth

unread,
Jul 6, 2010, 3:42:48 PM7/6/10
to Selenium Users
In my experience having the onclick portion of the href equal to any
string fixes this bug, not sure exactly what value is being set for it
here.

Ian

Salman Sabir

unread,
Feb 13, 2021, 6:54:19 AM2/13/21
to Selenium Users

<div class="cp-button" data-default-class="fa-plus" title="Add Transaction" orgtitle="Add" id="btnAdd2" onclick="chainPoint.main.doTheClick('btnAdd');" ishidden="false" style="display: inline-block;"><span class="cp-icon-only fa fa-plus "></span><div class="cp-icon-text cp-button-default-visible"><ul><li class="cp-btnIcon"><span class="cp-button-icon fa fa-plus"></span></li><li class="cp-btnText">Add Transaction</li></ul></div></div>

Above is the outerHTML

Following is my code in VBA but it doesn't work and my HTML CODE Has Jquery as well. Code runs without any error but doesn't click

driver.ExecuteScript("$('#btnAdd2').click()")

Reply all
Reply to author
Forward
0 new messages