Selenium HtmlUnitDriver 单击复选框

Selenium HtmlUnitDriver clicking on checkbox

我正在尝试在 运行 使用 selenium 时点击我的复选框。

我在使用 chromedriver 时没有问题 运行 我的测试。 但是当我切换到 HtmlUnitDriver 时,它会在点击复选框动作时抛出错误。抛出的错误是

org.openga.selenium.ElementNotVisibleException: You may only interact with visible elements

我尝试了多种方法,例如:

driver.findElement(By.xpath("//*[@id=\"chkConfirm\"]")).sendKeys(Keys.SPACE);
driver.findElement(By.xpath("//*[@id=\"chkConfirm\"]")).click();

但是 none 有效。有人可以帮我吗?

WebElement checkBox = driver.findElement(By.xpath("//*[@id='chkConfirm']"))
checkBox.isDisplayed();
if(!checkBox.isSelected())
checkBox.click();

试试这个街区。

你能在点击元素之前添加等待吗?请参阅下面的示例。

WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"chkConfirm\"]")));
driver.findElement(By.xpath("//*[@id=\"chkConfirm\"]")).click();

好的。我已经尝试了两个建议的答案,但 none 有效。

所以我决定转而使用 PhantomJS,它确实有效。

PhantomJS

谢谢大家!