How many ways to click on webElement In WebDriver?
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");
How to click on svg element in Selenium Java
The desired element is a svg element so to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:
selenium4 compatible code
Using cssSelector:
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div[placement='bottom'] svg"))).click();
Using xpath:
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@placement='bottom']//*[name()='svg']"))).click();
References
You can find a couple of relevant detailed discussions in:
- How to click on SVG elements using XPath and Selenium WebDriver through Java
How to click on a radio button using Selenium
Firs you will have to make sure that this id radioNTP
is unique in HTMLDOM or not.
Steps to check:
Press F12 in Chrome
-> go to element
section -> do a CTRL + F
-> then paste the //input[@id='radioNTP']
and see, if your desired element
is getting highlighted with 1/1
matching node.
If yes, then there are quite a few ways to perform click in Selenium.
Code trial 1:
Thread.sleep(5);
driver.findElement(By.xpath("//input[@id='radioNTP']")).click();
Code trial 2:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='radioNTP']"))).click();
Code trial 3:
Thread.sleep(5);
WebElement button = driver.findElement(By.xpath("//input[@id='radioNTP']"));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", button);
Code trial 4:
Thread.sleep(5);
WebElement button = driver.findElement(By.xpath("//input[@id='radioNTP']"));
new Actions(driver).moveToElement(button).click().build().perform();
If No, then you will have to change the locator and use the one which represent the desire node.
PS: Thread.sleep(5);
is just to solve this problem, if any of the above code works, Please replace the web element with WebDriverWait
.
Clicking Custom Element using Selenium Python
To get XPath of an element on website page, you can right click on the HTML element in inspector and in the menu Copy, you will be able to Copy full XPath.
How to click on Close icon/element on the Facebook sign-up page using Selenium Java?
The desired element is an <img>
tag which is the preceding-sibling
of a <div>
tag which is the immediate ancestor of the <div>
tag with text as Sign Up
Solution
To click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:
xpath:
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[text()='Sign Up']//ancestor::div[1]//preceding-sibling::img[1]"))).click();
How to click an element in Selenium WebDriver using JavaScript?
Executing a click via JavaScript has some behaviors of which you should be aware. If for example, the code bound to the onclick
event of your element invokes window.alert()
, you may find your Selenium code hanging, depending on the implementation of the browser driver. That said, you can use the JavascriptExecutor
class to do this. My solution differs from others proposed, however, in that you can still use the WebDriver methods for locating the elements.
// Assume driver is a valid WebDriver instance that
// has been properly instantiated elsewhere.
WebElement element = driver.findElement(By.id("gbqfd"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
You should also note that you might be better off using the click()
method of the WebElement
interface, but disabling native events before instantiating your driver. This would accomplish the same goal (with the same potential limitations), but not force you to write and maintain your own JavaScript.
Related Topics
How to Build Jars from Intellij Properly
What's Wrong With Using == to Compare Floats in Java
How to Programmatically Compile and Instantiate a Java Class
What's Causing My Java.Net.Socketexception: Connection Reset
Replace a Character At a Specific Index in a String
Creating an Instance Using the Class Name and Calling Constructor
Java Order of Initialization and Instantiation
Restricting Jtextfield Input to Integers
How to Convert Jsonstring to Jsonobject in Java
What's the Difference Between ".Equals" and "=="
Convert a Json String to Object in Java Me
Add Leading Zeroes to Number in Java