How to click a element by Selenium
There are several ways to click a element by selenium base on different situations.
- normal way, everyone know it.
element.click();
2. in some versions of IE, click() doesn’t work,
we can use this: element.sendKeys(Keys.RETURN).
3 . if the page is too long and the element is not on the screen, we need to move to the element first.
Actions actions = new Actions(driver);
actions.moveToElement(Element);
element.click();
4. use javascript, suppose you found a element named elementA
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", elementA );
5. if you try every solution upon, but it still didn’t work, the last solution is click a position, but that need to calculate.
first find a element which can use normal way to get it.
let’s say it named knownElement. calculate the distance between knownElement and the element you want to click, Let’s say x=10, y=25 ( pix), then we can use Action builder to move mouse and click it.
Actions builder = new Actions(driver);
builder.moveToElement(knownElement, 10, 25).click().build().perform();