简体   繁体   中英

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. 在此处输入图片说明

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

在此处输入图片说明

You can click using coordinates

Coordinates co = element.getCoordinates();

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();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM