How to click on element using Selenium?

Click using Selenium WebDriver:

To click on an element using Selenium WebDriver, we first need to locate the element on the web page using one of the available locator strategies, such as ID, class name, XPath, CSS selector, etc.

driver.findElement(By.locator(” “)).click();

Explanation:

  • driver.findElement(By.<locator>): This part finds the element on the web page using a locator strategy provided by Selenium, such as ID, class name, CSS selector, etc. You replace <locator> with the appropriate locator strategy you want to use, for example, By.id("elementID"), By.className("elementClassName"), etc.
  • .click(): This part simulates a mouse click on the found element, performing the same action as if a user clicked on it manually.

So, putting it all together, driver.findElement(By.<locator>).click(); finds an element on the web page using a locator strategy and then clicks on it.