5

As I am aware that user can click on Particular Webelement by using click method and one more way like using Sendkey Method with ASCII Value for left Click.

By Click Method: driver.findElement(By.cssSelector(".dbl")).click();

By Ascii Value : driver.findElement(By.cssSelector(".dbl")).sendKey("ASCII VALUE FOR Left Click");

Apart from this is there a way to perform click action??

JRodDynamite
  • 12,325
  • 5
  • 43
  • 63
SacTan
  • 379
  • 2
  • 5
  • 18

6 Answers6

6

You can use:

yourelement.sendKeys(Keys.RETURN) or .sendKeys(Keys.ENTER) : which is an equivalent of focusing that element and hitting RETURN/ENTER on that element

Also, There are methods to do this using Javacript but it is not usually recommended:

using the non-native Javascript Executor:

((JavascriptExecutor) driver).executeScript("arguments[0].click();", yourelement);

or by using Javascript Library:

JavascriptLibrary jsLib = new JavascriptLibrary();`
jsLib.callEmbeddedSelenium(driver, "triggerMouseEventAt", we, "click", "0,0");
Prateek
  • 1,145
  • 1
  • 8
  • 21
3

Below are some methods that will be useful to click a button/Image.

WebDriver driver = new ChromeDriver();
    driver.get("http://newtours.demoaut.com");
    WebElement signOnImage = driver.findElement(By.xpath("//input[@type='image'][@name='login']"));

// direct method from the API which is recommended always
    signOnImage.click(); 

1 Using Return Key

    //signOnImage.sendKeys(Keys.RETURN); 

2 Using JavascriptExecutor

2.1 
    JavascriptExecutor js = (JavascriptExecutor)driver; 
    js.executeScript("arguments[0].click();", signOnImage);

2.2         
    JavascriptExecutor js = (JavascriptExecutor) driver; 
    js.executeScript("document.getElementsByName('login')[0].click()");

3 Using Actions class

3.1
    Actions actions = new Actions(driver);  
    actions.click(signOnImage).perform();
3.2
    Actions actions = new Actions(driver);
    actions.moveToElement(signOnImage).click().perform();
3.3 
    Actions actions = new Actions(driver); 
    actions.clickAndHold(signOnImage).release().perform();
3.4 
    Actions actions = new Actions(driver);
    actions.sendKeys(signOnImage, Keys.RETURN).perform();
Sankumarsingh
  • 9,889
  • 11
  • 50
  • 74
  • 1
    Action class examples are very helpful.I was aware of only three of the ways and luckily am able to click using the step actions.sendKeys(signOnImage, Keys.RETURN).perform(); – Vamshi G May 03 '19 at 16:36
1

submit(); If the current element is a form, or an element within a form, then this will be submitted to the remote server. If this causes the current page to change, then this method will block until the new page is loaded

Pankaj Dubey
  • 279
  • 2
  • 18
  • Thank you Pankaj For your quick response. I agree but submit() will not work for all WebElement mean it will work only in case if it is form. – SacTan Mar 03 '16 at 11:45
  • Yes Prateek, It's designed to handle this only. Can you please elaborate the scenario like whats the need of looking for any alternate way if click() method solves the motive. May be I can help you with that. – Pankaj Dubey Mar 03 '16 at 11:49
  • I faced this question in interview, as I am aware about only two-three ways, so just curious to know about multiple ways of doing it. – SacTan Mar 03 '16 at 11:52
  • 1
    In that case I'll suggest just go through the below link and make a note of all important points. It has full detail of classes and functions available in selenium. URL: http://seleniumhq.github.io/selenium/docs/api/java/index.html – Pankaj Dubey Mar 03 '16 at 12:01
  • Thank You Pankaj for sharing the link. – SacTan Mar 03 '16 at 12:03
1

There are four typical ways to perform click in Selenium-Java bindings.

  1. Using findElement

    driver.findElement(By.xpath("//span[text()='Excel']")).click();
    
  2. Using WebDriverWait

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Excel']"))).click();
    
  3. Using executeScript

    WebElement button = driver.findElement(By.xpath("//span[text()='Excel']"));
    ((JavascriptExecutor)driver).executeScript("arguments[0].click();", button);
    
  4. Using ActionClass

    WebElement button  = driver.findElement(By.xpath("//span[text()='Excel']/parent::button[@aria-controls='report'][contains(@class,'downloadExcel')]"));
    new Actions(driver).moveToElement(button).click().build().perform();
    

I am using xpath, you can use css, linkText, tagName, name, partialLinkText etc as well to perform click.

-1

If you want to click a button or set an value to an web element through selenium you can use XPATH variable ,for using XPATH variable you must find the value of it ,you can find it using Firefox browser and few add_on's like firebugs .

driver.findElement(By.xpath(".//*[@id='main']/div[4]/div/button")).click();

I would suggest you to use XPATH variable so that you can locate any web element in a webpage.

If you want find the hyperlink web element then you can use By.linkText when you are sure about the tag name or choose By.partialLinkText by which you can locate even if you partial web element name but in this case your partial search key matches more than one element then By.partialLinkText won't works fine. For example,in case you knows the complete tag name of hyperlink use can use

driver.findElement(By.linkText("Click to Next Page")).click();

or else

Where you knows only a partial tag name

driver.findElement(By.linkText("Next Page")).click();

The secound option will not help you in all instances.

Ranjith
  • 89
  • 1
  • 4
-1

HTMLElement.click()

The HTMLElement.click() method simulates a mouse click on an element. At the core level when click() is used with supported elements e.g. <a>, <button>, <input>, etc, it fires the element's click event. This event then bubbles up to elements higher in the document tree (or event chain) and fires their click events.


There are several ways to invoke the click() method as follows:

  1. Element click(): This Element Click command scrolls into view the element if it is not already pointer-interactable, and clicks its in-view center point. An example:

    driver.findElement(By.linkText("Selenium")).click();
    
  2. Element sendKeys(Keys.SPACE): An example:

    driver.findElement(By.linkText("Selenium")).sendKeys(Keys.SPACE);
    
  3. Actions click(): Clicks at the current mouse location. Useful when combined with moveToElement(org.openqa.selenium.WebElement, int, int) or moveByOffset(int, int). An example:

    new Actions(driver).click(element).build().perform();
    
  4. Actions doubleClick(): Performs a double-click at the current mouse location. An example:

    new Actions(driver).keyDown(Keys.CONTROL).doubleClick(link).keyUp(Keys.CONTROL).build().perform();
    
  5. Action sendKeys(textToSend, Keys.RETURN): An example:

    new Actions(driver).moveToElement(element).sendKeys(textToSend, Keys.RETURN).perform();
    
  6. Action sendKeys(textToSend, Keys.ENTER): An example:

    new Actions(driver).moveToElement(element).sendKeys(textToSend, Keys.ENTER).perform();
    
  7. Actions contextClick(): Performs a context-click at the current mouse location. An example:

    new Actions(driver).contextClick(element).build().perform();
    
  8. JavascriptExecutor executeScript​(): Executes JavaScript in the context of the currently selected frame or window. The script fragment provided will be executed as the body of an anonymous function.

    ((JavascriptExecutor)driver).executeScript("arguments[0].click()", webElement);
    
  9. Submit(): Applicable if this current element is a form, or an element within a form.

    WebElement searchButton = driver.findElement(By.xpath("//button[@type='submit']"));
    searchButton.submit();
    
  10. Mouse Double Click

  11. Mouse Triple Click

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352