Reputation: 306
Here is my question: I try to click webdriver to a checkbox. Firstly I tried to click via using xpath, cssSelector, classname etc. I got the paths with firebug but compiler complains. It says no such element. So I try to solve problem in different way, When I send two times Tab Key, my selection comes on checkbox. Without using enter or space key, how to click on it.(I try to handle with Google Recaptcha, so if I use space or enter keys, It detects that I'm machine.) Here is a part from my java code
Actions action = new Actions(driver);
action.sendKeys(Keys.TAB).build().perform();
Thread.sleep(1000);
action.sendKeys(Keys.TAB).build().perform();
Thread.sleep(1000);
System.out.println("Tabbed");
action.click().build().perform();//Try to click on checkbox but it clicks on somewhere space.
I'm waiting for your helps. Thank you
Upvotes: 0
Views: 700
Reputation: 2436
You need to move the mouse cursor to the element location before you execute "click". Additionally it might help if you wait until the element becomes visible.
WebElement element = driver.findElement(By.cssSelector("..."));
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(element)); //wait until the element is visible
action.moveToElement(element).click().perform();
If you absolutely want to avoid finding the elements by CSS/XPath, you will need to find out the coordinates of the element. Paste the following script into your chrome/firefox console and press enter:
document.onclick = function(e)
{
x = e.pageX;
y = e.pageY + 75; // navigation bar offset, you may need to change this value
console.log("clicked at position (x, y) " + x + ", " + y);
};
Now you can click on the check box on your website and the position will be printed in the console. The obtained position can be used via the Robot class:
Robot robot = new Robot(); //java.awt.Robot
robot.mouseMove(x, y); // moves your cursor to the supplied absolute position on the screen
robot.mousePress(InputEvent.BUTTON1_MASK); //execute mouse click
robot.mouseRelease(InputEvent.BUTTON1_MASK); //java.awt.event.InputEvent
Note that this will really move your cursor to the absolute coordinates. This means it will be problematic if the resolution changes or if you need to scroll to the element. You also should maximize the browser while getting the coordinates and using the WebDriver (driver.manage().window().maximize()
), otherwise the coordinates won't match.
Upvotes: 2