简体   繁体   中英

Selenium Java - mark checkbox selected without using .click()

I want mark some checkboxes as checked using Selenium and Java, but in the .css style sheet their "width" and "height" is set to "100", yet in the browser they appear as normal checkboxes. Because of this selenium finds them and succesfully executes .click() function, but the checkbox does not get selected. Is there a way to simply set the checkbox as selected without using .click() ?

Difficult to say without a reproducible sample, but you may try clicking via javascript :

WebElement checkbox = driver.findElement(By.ID("mycheckbox"));

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", checkbox);

See here the differences:

Im afraid there is no select() method on a check box, but you could write something like this and reuse it.. which will abstract the operation of select

if ( !driver.findElement(By.id("idOfTheElement")).isSelected() )
{
    driver.findElement(By.id("idOfTheElement")).click();
}

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