How to click a element using JavascriptExecutor on webdriver?

14,078 views
Skip to first unread message

GaryLee

unread,
Apr 19, 2012, 10:50:55 AM4/19/12
to seleniu...@googlegroups.com
HTML:
<span class="s_btn_wr">
<input id="su" class="s_btn" type="submit" onmouseout="this.className='s_btn'" onmousedown="this.className='s_btn s_btn_h'" value="Search">
</span>

JavascriptExecutor js = (JavascriptExecutor) driver;
String label = (String) js.executeScript(
                "javascript:document.getElementById('su').onclick()");


FAILED:
org.openqa.selenium.WebDriverException: document.getElementById("su").onclick is not a function
How to write the js?

Mike Riley

unread,
Apr 19, 2012, 12:20:11 PM4/19/12
to seleniu...@googlegroups.com
Why are you trying to do this with JavaScript?

driver.findElement(By.id("su")).click();

No need to resort to JavaScript to simply do a click.

Mike

GaryLee

unread,
Apr 19, 2012, 10:00:39 PM4/19/12
to seleniu...@googlegroups.com
I just wanna to learn some basic knowledge about js,that will help me on a lot ,especially on android browser that driven by js.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/2aSULsikJyEJ.
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.

Mark Collin

unread,
Apr 20, 2012, 6:08:30 AM4/20/12
to seleniu...@googlegroups.com

I would suggest avoiding the JavaScript executor unless absolutely necessary.  The Selenium dev team have put a lot of work into making things like .click() work.  If you roll your own version you are basically ignoring the work they have done and trying to implement your own version.  I would suggest that 99% of the time you will come up with a solution that is not as robust/stable and likely has problems that they have already identified and fixed.

somesh bansal

unread,
Jul 17, 2012, 9:27:16 AM7/17/12
to seleniu...@googlegroups.com
 Hi Mike,
 
 I have a scenario in which the element (My social groups link) is not visible, unless the mouseover is done on Groups (link-as attached in    
 screenshot)
 
 Now, when I run it following commands
 selenium.click("link=My Working Groups");
        selenium.waitForPageToLoad("30000");
        selenium.click("//a[contains(text(),'My Working Groups')]");
        selenium.waitForPageToLoad("30000");
        selenium.click("//ul[@id='nav']/li[3]/ul/li[2]/a");
        selenium.waitForPageToLoad("30000");
        selenium.click("//a[contains(@href, 'https://lms2-qa-2gu-gu.2tor.com/local/groups/mygroups.php?type=academic')]");
        selenium.waitForPageToLoad("30000");
        selenium.click("//li[3]/ul/li[2]/a");
        selenium.waitForPageToLoad("30000");

     OR

        driver.findElement(By.linkText("My Working Groups")).click();
        driver.findElement(By.xpath("//a[contains(text(),'My Working Groups')]")).click();
        driver.findElement(By.xpath("//ul[@id='nav']/li[3]/ul/li[2]/a")).click();
        driver.findElement(By.xpath("//a[contains(@href, 'https://lms2-qa-2gu-gu.2tor.com/local/groups/mygroups.php?type=academic')]")).click();
        driver.findElement(By.xpath("//li[3]/ul/li[2]/a")).click();

 It throws me an exception saying Element is currently not visible

 Please guide me as how to make the element visible, through JavascriptExecutor in my script

 Regards
 Somesh 
ElementNotVisible.PNG

Peter Gale

unread,
Jul 17, 2012, 9:31:32 AM7/17/12
to Selenium Users
Shouldn't you just be doing whatever a real user does to make the element visible?


Date: Tue, 17 Jul 2012 06:27:16 -0700
From: sbans...@gmail.com
To: seleniu...@googlegroups.com
Subject: [selenium-users] Re: How to click a element using JavascriptExecutor on webdriver?
--
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.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/G9fOTRlSr0UJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

somesh bansal

unread,
Jul 17, 2012, 10:05:51 AM7/17/12
to seleniu...@googlegroups.com

 Hi Peter,

 Can you please put some on light on the same

 Somesh

On Tuesday, July 17, 2012 7:01:32 PM UTC+5:30, PeterJef...@Hotmail.co.uk wrote:
Shouldn't you just be doing whatever a real user does to make the element visible?


Date: Tue, 17 Jul 2012 06:27:16 -0700
From: sbans...@gmail.com
To post to this group, send email to selenium-users@googlegroups.com.
To unsubscribe from this group, send email to selenium-users+unsubscribe@googlegroups.com.

Peter Gale

unread,
Jul 17, 2012, 10:10:02 AM7/17/12
to Selenium Users
Not sure what you mean Somesh.

What action would a real user take that makes the element visible?

OR

Why are trying to interact with the page in a way that a real user would not do?


Date: Tue, 17 Jul 2012 07:05:51 -0700
From: sbans...@gmail.com
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] Re: How to click a element using JavascriptExecutor on webdriver?
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/vVpefEX2ZJoJ.

somesh bansal

unread,
Jul 17, 2012, 10:21:53 AM7/17/12
to seleniu...@googlegroups.com
 
 Sorry peter, I was just asking if you can explain in detail.

 Real user will take the mouse over the mainlink, and then click on respective link.. I tried the same but
 selenium.mouseover(); is not working

 Somesh


On Tuesday, July 17, 2012 7:40:02 PM UTC+5:30, PeterJef...@Hotmail.co.uk wrote:
Not sure what you mean Somesh.

What action would a real user take that makes the element visible?

OR

Why are trying to interact with the page in a way that a real user would not do?


Date: Tue, 17 Jul 2012 07:05:51 -0700
From: sbans...@gmail.com

Peter Gale

unread,
Jul 17, 2012, 10:32:57 AM7/17/12
to Selenium Users
That sounds a strange action to have to take, rather than just clicking on something.

Obviously, you really need to see why the mouseover action is not working. That's not somethign I've used myself, but there have been lots of threads on action builders, which might be useful.

In the mean time, I presume a "onmouseout" event it the trigger, so you could run something like:


    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("javascript:document.getElementById('id').onmouseout()");



Date: Tue, 17 Jul 2012 07:21:53 -0700
From: sbans...@gmail.com
To: seleniu...@googlegroups.com
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/I578tbKXyDsJ.

Mike Riley

unread,
Jul 17, 2012, 2:28:27 PM7/17/12
to seleniu...@googlegroups.com
What isn't clear here is if you are using the Selenium 1.0 API or WebDriverBackedSelenium (since you are also showing some WebDriver code below).  Also, which version of Selenium are you using?

Your code example shows no mouse over logic in either case.  You have to do the mouse over logic on the Groups menu item, which would come before this code you showed us.  You must then wait for it to become visible.

This is why I have a method that I pass two locator strings to called mouseOverAndWait().  It hovers over the first locator and waits for the second one to become visible.  It also does a wait for the one to hover over to be present before trying to do the hover.

So my Selenium 1.0 code looks like this (using my methods for waiting for present and visible):
waitForElementPresent(hoverLocator, timeout);
selenium.mouseOver(hoverLocator);
waitUntilVisible(waitLocator);

The same code for WebDriver looks like this:
Actions userAction = new Actions(driver);
WebElement element = waitForElementPresent(By.xpath(hoverLocator),
                                           timeout);   // Wait for the hover element
userAction.moveToElement(element).perform();
waitUntilVisible(element, timeout);

If you insist on using JavaScript, look at comment 60 here:
http://code.google.com/p/selenium/issues/detail?id=2067

The equivalent code using JavaScript is:

JavascriptExecutor js = (JavascriptExecutor) driver;
String mouseOverScript =
                "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover', true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}";
WebElement element = waitForElementPresent(By.xpath(hoverLocator),
                                           timeout);   // Wait for the hover element
js.executeScript(mouseOverScript, element);
waitUntilVisible(waitLocator, timeout);

For WebDriver all the waitUntilVisible() comes down to is a loop checking element.isDisplayed().  For Selenium 1.0 it is a loop checking selenium.isVisible(locator).

Mike

Somesh Bansal

unread,
Aug 10, 2012, 8:11:07 AM8/10/12
to seleniu...@googlegroups.com
Hi Peter & Mike,

Sorry for delay response!!! Thank you for your support... 

I used action builder command

Getting following problems
1>  and was able to run the same on WINDOWS machine. It is not that consistent, get fails sometimes.

2> Unable to run the same on LINUX machine even once... This is on PRIORITY for me as of now

Please provide your kind inputs

Regards
Somesh

--
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.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/ZdmszAgBtywJ.

Somesh Bansal

unread,
Aug 10, 2012, 2:34:16 PM8/10/12
to seleniu...@googlegroups.com
I am running following code on windows machine and working fine

                WebElement visibleElememt = driver.findElement(By.linkText(menu));

//the element that I want to click (hidden)
WebElement hiddenElement = driver.findElement(By.xpath(menuOption));

//build and perform the mouseOver with Advanced User Interactions API
Actions builder2 = new Actions(driver);
for (int i = 0;; i++) {
System.out.println("I OutNumber:" +i);
if (i >= 60) fail("");
try { 
builder2.moveToElement(visibleElememt).perform();
if (hiddenElement.isDisplayed())
{
hiddenElement.click();
System.out.println("I Number:" +i);
break;
}
catch (Exception e) {}
}*/

Somesh Bansal

unread,
Aug 10, 2012, 2:35:52 PM8/10/12
to seleniu...@googlegroups.com
I am running following code on windows machine and working fine

                //get the element that shows menu with the mouseOver event
                WebElement visibleElememt = driver.findElement(By.linkText(menu));

//the element that I want to click (hidden)
WebElement hiddenElement = driver.findElement(By.xpath(menuOption));

//build and perform the mouseOver with Advanced User Interactions API
Actions builder2 = new Actions(driver);
for (int i = 0;; i++) {
System.out.println("I OutNumber:" +i);
if (i >= 60) fail("");
try { 
builder2.moveToElement(visibleElememt).build.perform();
if (hiddenElement.isDisplayed())
{
hiddenElement.click();
System.out.println("I Number:" +i);
break;
}
catch (Exception e) {}
}

It would be great, if you can help me with JavascriptExecutor code so, I can try the same

Somesh

On Fri, Aug 10, 2012 at 5:41 PM, Somesh Bansal <sbans...@gmail.com> wrote:

Saurabh Mishra

unread,
Nov 27, 2013, 11:24:49 PM11/27/13
to seleniu...@googlegroups.com
Yes mike you are right but what if I need to select the time from that drop down?

Mukul Bansal

unread,
Mar 7, 2015, 11:22:08 PM3/7/15
to seleniu...@googlegroups.com
This will always work
public void click(WebElement a,WebElement b)
{
try 
{
String mouseOverScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover',true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}";
((JavascriptExecutor) driver).executeScript(mouseOverScript,a);
Thread.sleep(1000);
((JavascriptExecutor) driver).executeScript(mouseOverScript,b);
Thread.sleep(1000);
((JavascriptExecutor)driver).executeScript("arguments[0].click();",b);
} catch (Exception e) {
// TODO: handle exception
Reply all
Reply to author
Forward
0 new messages