
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
WebDriver click() vs JavaScript click().
We can click a link with the webdriver click and Javascript click. For the Selenium webdriver click of a link we can use link text and partial link text locator. We can use the methods driver.findElement(By.linkText()) and driver.findElement(By.partialLinkText()) to click.
The links in an html code are enclosed in an anchor tag. The link text enclosed within the anchor tag is passed as argument to the driver.findElement(By.linkText(<link text>)) method. The partial matching link text enclosed within the anchor tag is passed as argument to the driver.findElement(By.partialLinkText(<partial link text>)) method. Finally to click on the link the click method is used.
Let us see the html code of a link having the anchor tag.
Example
Code Implementation.
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.By; public class DriverClick{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://www.tutorialspoint.com/about/about_careers.htm"); // identify link with link text locator driver.findElement(By.linkText("Write for us")).click(); System.out.println("Page title after click: " + driver.getTitle()); } }
We can also perform web operations like clicking on a link with Javascript Executor in Selenium. We shall use the executeScript method and pass argument index.click() and webelement to be clicked as arguments to the method.
Example
Code Implementation with Javascript executor.
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.By; public class DriverClickJs{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://www.tutorialspoint.com/about/about_careers.htm"); // identify link WebElement l = driver.findElement(By.linkText("Write for us")); //click link with Javascript Executor JavascriptExecutor j = (JavascriptExecutor) driver; j.executeScript("arguments[0].click();", l); System.out.println("Page title after click: " + driver.getTitle()); } }
Output
- Related Questions & Answers
- Selenium Webdriver submit() vs click().
- How to use Selenium webdriver to click google search?
- How to click on hidden element in Selenium WebDriver?
- How to click on image in selenium webdriver Python?
- How to click Allow on Show Notifications popup using Selenium Webdriver?
- How to click on a link using Selenium webdriver in Python.
- HTML DOM click() method
- jQuery click() with Example
- Click-On ARM Prothesis
- How can I trigger a JavaScript click event?
- How to call a JavaScript function on click?
- Difference between addEventListener and on-click in JavaScript
- How do you click on an element which is hidden using Selenium WebDriver?
- Clicking an element using javascript vs actions vs webdriver?
- Alternative of click() in Selenium