【问题标题】:Selenium HtmlUnitDriver clicking on checkboxSelenium HtmlUnitDriver 点击复选框
【发布时间】:2017-12-07 09:16:55
【问题描述】:
我试图在使用 selenium 运行时点击我的复选框。
我在使用 chromedriver 时运行测试没有问题。
但是当我切换到HtmlUnitDriver 时,它会在点击复选框动作时抛出错误。抛出的错误是
org.openga.selenium.ElementNotVisibleException: 你只能与可见元素交互
我尝试了多种方法,例如:
driver.findElement(By.xpath("//*[@id=\"chkConfirm\"]")).sendKeys(Keys.SPACE);
driver.findElement(By.xpath("//*[@id=\"chkConfirm\"]")).click();
但这些都不起作用。有人可以帮帮我吗?
【问题讨论】:
标签:
java
selenium
xpath
checkbox
htmlunit
【解决方案1】:
您可以在单击元素之前添加等待吗?请参阅下面的示例。
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"chkConfirm\"]")));
driver.findElement(By.xpath("//*[@id=\"chkConfirm\"]")).click();
【解决方案2】:
好的。我已经尝试了两个建议的答案,但都没有。
所以我决定继续使用 PhantomJS,它确实有效。
PhantomJS
谢谢大家!
【解决方案3】:
WebElement checkBox = driver.findElement(By.xpath("//*[@id='chkConfirm']"))
checkBox.isDisplayed();
if(!checkBox.isSelected())
checkBox.click();
试试这个块。