visit
Selenium offers a ‘.click()’ method to help you perform various mouse-based operations for your web-application. You can use the Selenium click button method for various purposes such as selecting the radio button and checkbox or simply clicking on any button or link, drag and drop, click and hold, etc.
In the next section, I am going to demonstrate the practical implementation of Selenium click button method in for basic operations, and advanced operations. After that, I will also show you how you can use CSS selectors with the Selenium .click method.The most basic operation using a Selenium click button method is a left-click or a mouse click.
Test Scenario: Visit LambdaTest Homepage and click on the Login button.
driver.get("//www.lambdatest.com/");
driver.findElement(By.linkText("Login")).click(); //using Selenium click button method
Test Scenario 2: After you click on the LambdaTest Login button, tick the checkbox for “Remember me”.
WebElement chkBx = driver.findElement(By.className("mycheckbox"));
boolean flag2 = chkBx.isSelected();
System.out.println(flag2);
if(flag2) //true
{
System.out.println("checkbox is already selected");
}
else //false
{
System.out.println("select checkbox");
chkBx.click();
Note: To confirm whether a checkbox or a radio button is selected or not, we make us of the isSelected().
Test Scenario: Go to LambdaTest Live and open the video in a new tab.
package postBlo;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class Lambda {
public static void main(String[] args) throws InterruptedException, AWTException {
System.setProperty("webdriver.chrome.driver", "./exefiles/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("//www.lambdatest.com/feature");
Actions actions = new Actions(driver);
WebElement elementLocator = driver.findElement(By.xpath("//*[@id=\"vid\"]"));
actions.contextClick(elementLocator).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).build().perform();
Actions action = new Actions(driver);
action.contextClick(elementLocator).build().perform();
Robot robot = new Robot();
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_DOWN);
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_DOWN);
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_DOWN);
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_ENTER);
}
}
Test Scenario: Visit LambdaTest Pricing and perform a mouse hover on the text “What are parallel tests?”. After understanding what parallel tests are picking a parallel test value from the drop-down.
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement el = driver.findElement(By.id("id"));
js.executeScript("arguments[0].scrollIntoView();", el);
Example Script for Test Scenario:
package postBlo;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class MouseHover {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "./exefiles/chromedriver.exe");
// ChromeDriver c = new ChromeDriver();
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("//www.lambdatest.com/pricing");
((JavascriptExecutor) driver).executeScript("scroll(0,300)");
Actions ac = new Actions(driver);
WebElement wb = driver.findElement(By.xpath("/html/body/div[2]/section[1]/div/div[2]/div[2]/div[1]/div/div/div[3]/p"));
ac.moveToElement(wb).build().perform();
Thread.sleep(3000);
WebElement plan = driver.findElement(By.xpath("/html/body/div[2]/section[1]/div/div[2]/div[2]/div[2]/div/div/div[2]/div/div/button"));
plan.click();
Thread.sleep(2000);
driver.findElement(By.xpath("//a[text()='4 Parallel Tests(25 Users)']")).click();
Thread.sleep(4000);
driver.quit();
}
}
We can use movebyOffset to perform a click anywhere on the page by feeding (x,y) coordinates to the WebDriver. You can even generate a click on blank portions of your webpage as long as you are aware of the (X,Y) coordinates.
Test Scenario: Visit LambdaTest Homepage and find the (x,y) coordinates for the button “Start Free Testing”.
In the above image, you can notice the X, Y coordinates specified under the highlighted “Start Free Testing” button.
driver.get("//www.lambdatest.com");
Actions act = new Actions(driver);
act.moveByOffset(913, 477).contextClick().build().perform();
Note: These (X, Y) coordinates will be different from one screen resolution and screen size to another.
driver.get("//www.lambdatest.com");
Actions act = new Actions(driver);
//Double click on element
WebElement wb2 = driver.findElement(By.xpath("/html/body/div[2]/section[1]/div/div/h1"));
act.doubleClick(wb2).perform();
Test Scenario: Visit Interface, perform a click and hold over the draggable box and then move using the offset.
driver.get("//jqueryui.com/draggable/");
driver.manage().window().maximize();
Actions action = new Actions(driver);
driver.switchTo().frame(0);
WebElement ele = driver.findElement(By.id("draggable"));
action.moveToElement(ele).clickAndHold().moveByOffset(310, 220).build().perform();
Thread.sleep(4000);
driver.quit();
Test Scenario: Visit Interface, perform a click on the draggable box, and then drop it in the container box.
driver.get("//jqueryui.com/droppable/");
driver.manage().window().maximize();
Actions action = new Actions(driver);
driver.switchTo().frame(0);
WebElement drag = driver.findElement(By.id("draggable"));
WebElement drop = driver.findElement(By.id("droppable"));
action.dragAndDrop(drag, drop).build().perform();
Thread.sleep(4000);
driver.quit();
driver.findelement (by.id(“idLocator”)).click();
driver.findelement (by.name(“password”).sendkeys(“123456789”);
driver.findelement (by.class.name(“mycheckbox”)).click();
Whenever there is a webelement with unique HTML tag we can use the TagName locator in Selenium automation testing. There can be many types of Unique HTML tag such as:
Hyperlink – < a >
Image – < img >
Header – < h1 >
So for example, if you have a unique Hyperlink TagName using the anchor tag then you can incorporate Selenium click button interaction using the below code.
driver.findelement(by.tagname(“a”)).click();
driver.findelement(by.linktext(“facebook”).click();
driver.findelement(by.partiallinktext(“zon”)).click();
driver.findElement(By.cssSelector("#app > section > form > div > div > input:nth-child(3)")).click();
Note: Whenever we use CSS selector or Xpath as a locator we should not use mandatory attribute because mandatory attribute will be the same for similar kind of webelement so it is always recommended to go for additional attributes.
driver.findElement(By.xpath("//*[@id=\"app\"]/section/form/div/div/input[1]")).click();
Test Scenario: Visit LambdaTest Homepage and click on every hyperlink invoked through the anchor tag in HTML.
Below is a script which is generating a list of hyperlinks on the LambdaTest Homepage where the tagName is “a” which represents an anchor tag. After that, it would click on every link a single time, in random order. The script would visit the first link, go to the respective web page and then will roll back to LambdaTest to visit the next link and the cycle will go on till every link is visited a single time.package pack1;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class ClickRandom {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "./exefiles/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("//www.lambdatest.com/");
List<WebElement> link = driver.findElements(By.tagName("a"));
int linkcount = link.size();
System.out.println(linkcount);
for (int i = 0; i < linkcount; i++) {
String linktext = link.get(i).getText();
System.out.println(linktext);
}
Random r = new Random();
link.get(r.nextInt(2)).click();
String title = driver.getTitle();
System.out.println("Page Title is " + title);
Thread.sleep(5000);
driver.get("//www.lambdatest.com/");
}
}
Console Output:
package postBlo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class ClickMultipleOnLink {
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "./exefiles/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("//accounts.lambdatest.com/login");
driver.findElement(By.name("email")).sendKeys("[email protected]");
driver.findElement(By.name("password")).sendKeys("1234567");
driver.findElement(By.xpath("/html/body/div[1]/section/form/div/div/button\r\n" +
"")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("/html/body/div[2]/header/aside/ul/li[2]/a")).click();
Thread.sleep(2000);
for (int i = 0; i < 75; i++) {
driver.findElement(By.id("navbarDropdown")).click();
Thread.sleep(1000);
}
}
}
By far, I hope you have a good understanding of how powerful the Selenium .click() button method can be and how you can leverage it. I hope the bonus tips were helpful for your daily scripts. Do you want to share a bonus tip too or a unique way in which you used the Selenium click button method? If so, drop your thoughts in the comment section below!
Previously published at