Tree55Topz
Tree55Topz

Reputation: 1142

Selenium / ChromeDriver - Clicking in the center of an element

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. enter image description here

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..

enter image description here

Upvotes: 5

Views: 6710

Answers (3)

Ajeet Yadawa
Ajeet Yadawa

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

Arik G
Arik G

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

Bhumi Panchal
Bhumi Panchal

Reputation: 69

You can click using coordinates

Coordinates co = element.getCoordinates();

Upvotes: 2

Related Questions