Reputation: 1142
So I ran into a new issue today that I have not experienced for, and it relates to the nature of the Chrome Driver (I believe Chrome is the only one that does this..). I am aware that when you click an element using .click() it clicks in the center. However this is troubling because I am trying to click a checkbox that just so happens to have a link nested in the center.
I have tried using the JavaScript Executor as well and no luck.. Does anyone know a way around this? Yes I have tried just accessing the box but it doesnt have an identifier I can use..
Upvotes: 5
Views: 6710
Reputation: 163
This issue could be resolved by two methods
Method 1: Find correct xpath of checkboxx use default click
driver.findElement(By.xpath("CheckBoxXPath").click();
Method 2: If you really want to click on centre of WebElement then you Actions class method click(WebElement target) this method click on mid of WebElement. Refer How to use Actions class clcik method
Upvotes: 2
Reputation: 66
Since you didn't add here the check box html I will assume it is something like:
<input type="checkbox" id="checkbox_id">
<label for="checkbox_id">Something</label>
So you just have to click the input and not the label. It will look like this:
driver.findElemnt(By.id("checkbox_id")).click();
Or using xPath:
driver.findElemnt(By.xpath("//input[@type='checkbox']")).click();
Upvotes: 1
Reputation: 69
You can click using coordinates
Coordinates co = element.getCoordinates();
Upvotes: 2