How to use JavaScript in selenium to click an Element?

0 votes

I was using following code/html:

<button name="btnG" class="gbqfb" aria-label="Google Search" id="gbqfb"><span class="gbqfi"></span></button>

This is my Java code for clicking the element using selenium webdriver.

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

How can I implement this using Java Script and Webdriver?

Apr 13, 2018 in Selenium by Meci Matt
• 9,460 points
22,795 views

2 answers to this question.

0 votes

Executing a click via JavaScript has some behaviors of which you should be aware. If, for example, the code bound to the onclick event of your element invokes window.alert(), you may find your Selenium code hanging, depending on the implementation of the browser driver. That said, you can use the JavascriptExecutor class to do this. My solution differs from others proposed, however, in that you can still use the WebDriver methods for locating the elements.

// Assume driver is a valid WebDriver instance that
// has been properly instantiated elsewhere.
WebElement element = driver.findElement(By.id("gbqfd"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

You should also note that you might be better off using the click() method of the WebElementinterface, but disabling native events before instantiating your driver. This would accomplish the same goal (with the same potential limitations), but not force you to write and maintain your own JavaScript.

answered Apr 13, 2018 by Shubham
• 13,380 points
0 votes
JavascriptExecutor js = (JavascriptExecutor) driver;  
js.executeScript(Script,Arguments);

js.executeScript("document.getElementById('gbqfb"').click();");
answered Apr 1 by anonymous

Related Questions In Selenium

0 votes
1 answer

How can we use JavaScript Executor to click and enter data to a web element in Selenium?

WebElement element = driver.findElement(By.id("abcd")); // Let the ...READ MORE

answered Mar 21 in Selenium by Lakshmi Sarvepalli
484 views
0 votes
1 answer
0 votes
2 answers

Unable to Click on an Element in Selenium (Python) even after finding it.

Here, I give you working script which ...READ MORE

answered Sep 19, 2018 in Selenium by Priyaj
• 57,530 points
13,806 views
0 votes
1 answer

How to use link as a locator to find element in selenium?

Hello @Jino, its amazing to explore ways ...READ MORE

answered Dec 21, 2018 in Selenium by Nabarupa
204 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,510 points
2,574 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,870 points
2,037 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,870 points
2,131 views
0 votes
1 answer

Selenium Type method test method failed

In this matter ur 1st paramter should ...READ MORE

answered Apr 3, 2018 in Selenium by Shubham
• 13,380 points
185 views
0 votes
1 answer

Downloading *.jar file from http:// seleniumhq.org using Selenium WebDriver

For Selenium Standalone Server: profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/java-archive"); If downloading ...READ MORE

answered Apr 17, 2018 in Selenium by Shubham
• 13,380 points
1,708 views