简体   繁体   English

不使用.click()选中Selenium Java-mark复选框

[英]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. 我想将某些复选框标记为使用Selenium和Java选中,但是在.css样式表中,它们的“宽度”和“高度”设置为“ 100”,但是在浏览器中它们却显示为普通复选框。 Because of this selenium finds them and succesfully executes .click() function, but the checkbox does not get selected. 因此,硒找到了它们并成功执行了.click()函数,但是未选中该复选框。 Is there a way to simply set the checkbox as selected without using .click() ? 有没有一种方法可以简单地将复选框设置为选中状态而不使用.click()?

Difficult to say without a reproducible sample, but you may try clicking via javascript : 没有可复制的样本很难说,但您可以尝试通过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 恐怕复选框上没有select()方法,但是您可以编写像这样的东西并重用它..这将抽象select的操作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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