how to click on web element in selenium

Selenium provides the click() method to simulate a mouse click on a web element. This method can be applied to any interactable web element (such as buttons, links, checkboxes, or radio buttons) that has been successfully located using Selenium locators.

Before calling click(), ensure that:

  • The element is present in the DOM
  • The element is visible
  • The element is enabled (clickable)

Failing to meet these conditions may result in exceptions such as ElementNotInteractableException or ElementClickInterceptedException.

Click in Selenium

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.time.Duration;

public class ClickTest {

WebDriver driver;

@BeforeTest
public void setup() {
// Set ChromeDriver path if not already in system PATH
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://example.com");
}

@Test
public void clickOnElement() {

// Explicit wait
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

// Locate element and wait until clickable
WebElement button = wait.until(
ExpectedConditions.elementToBeClickable(By.id("loginBtn"))
);

// Click the element
button.click();
}

@AfterTest
public void tearDown() {
// Close browser
driver.quit();
}
}

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using System;

namespace SeleniumClickTest
{
public class ClickTest
{
IWebDriver driver;

[SetUp]
public void Setup()
{
driver = new ChromeDriver();
driver.Manage().Window.Maximize();
driver.Navigate().GoToUrl("https://example.com");
}

[Test]
public void ClickOnElement()
{
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));

IWebElement button = wait.Until(
SeleniumExtras.WaitHelpers.ExpectedConditions
.ElementToBeClickable(By.Id("loginBtn"))
);

button.Click();
}

[TearDown]
public void TearDown()
{
driver.Quit();
}
}
}

const { Builder, By, until } = require("selenium-webdriver");
require("chromedriver");
const assert = require("assert");

describe("Selenium Click Test", function () {

let driver;

// Increase timeout for Selenium
this.timeout(30000);

before(async function () {
driver = await new Builder().forBrowser("chrome").build();
await driver.manage().window().maximize();
await driver.get("https://example.com");
});

it("Click on web element", async function () {

let button = await driver.wait(
until.elementLocated(By.id("loginBtn")),
10000
);

await driver.wait(until.elementIsVisible(button), 10000);
await button.click();
});

after(async function () {
await driver.quit();
});
});

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Testingtalkslatest.com - A project by CreativeHub IT Solutions.
Contact Us At: support@testingtalkslatest.com
Our Partner websites - Classified Hub , CodesToolbox , Smart Fitness Guide , CodesToolbox , Testing Forum
Scroll to Top
0
Would love your thoughts, please comment.x
()
x