Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Actions class - click(WebElement ele) function does not clicks

I am trying to use the click(WebElement) method of the Actions class to click on an element on the google homepage. The code runs successfully but the click event is not trigerred.

package p1;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;



public class ClickLink 
{
    static WebDriver driver;
    public static void main(String[] args)
    {
        try
        {
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("http://www.google.com/");
        WebElement icon = driver.findElement(By.xpath(".//*[@id='gbwa']/div[1]/a"));
        Actions ob = new Actions(driver);
        ob.click(icon);
        System.out.println("Link Clicked !!");
        Thread.sleep(10000);
        driver.close();
        }
        catch(Exception e)
        {
            System.out.println("Exception occurred : "+e);
            driver.close();
        }
    }
}

Here is the result when the above script is executed : [link]

However, when the same element is clicked using the click() method of the WebElement interface, then the click is trigerred.

package p1;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;

public class ClickLink 
{
    static WebDriver driver;
    public static void main(String[] args)
    {
        try
        {
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("http://www.google.com/");
        WebElement icon = driver.findElement(By.xpath(".//*[@id='gbwa']/div[1]/a"));
        icon.click();
        System.out.println("Link Clicked !!");
        Thread.sleep(10000);
        driver.close();
        }
        catch(Exception e)
        {
            System.out.println("Exception occurred : "+e);
            driver.close();
        }
    }
}

Here is the result when the above script is executed : [link]

Please let me know the reason as to why the click event is not trigerred and resolution for the same.

like image 282
Akash Nigam Avatar asked Sep 12 '25 00:09

Akash Nigam


1 Answers

You have made a simple mistake of not building and performing the Action. Note that you have created an instance of Actions class ob. As the name signifies the Actions class defines a set of sequential actions to be performed. So you have to build() your actions to create a single Action and then perform() the action.

The below code should work!!

WebElement icon = driver.findElement(By.xpath(".//*[@id='gbwa']/div[1]/a"));
Actions ob = new Actions(driver);
ob.click(icon);
Action action  = ob.build();
action.perform();

If you look at the code given below to first move to the icon element and then click the element would better explain the Actions class.

Actions ob = new Actions(driver);
ob.moveToElement(icon);
ob.click(icon);
Action action  = ob.build();
action.perform();
like image 179
Sighil Avatar answered Sep 13 '25 21:09

Sighil