问题
I'm trying to get my checkbox clicked while running using selenium.
I have no issue running my test when using chromedriver.
But when I switch to HtmlUnitDriver
, it will throw error when it reaches the clicking of checkbox action. The error thrown is
org.openga.selenium.ElementNotVisibleException: You may only interact with visible elements
I've tried multiple methods like:
driver.findElement(By.xpath("//*[@id=\"chkConfirm\"]")).sendKeys(Keys.SPACE);
driver.findElement(By.xpath("//*[@id=\"chkConfirm\"]")).click();
But none of it works. Can someone help me out?
回答1:
Could you add wait before clicking on the element? See an example below.
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"chkConfirm\"]")));
driver.findElement(By.xpath("//*[@id=\"chkConfirm\"]")).click();
回答2:
Okay. I've tried both the suggested answer but none works.
So I decided to move on to PhantomJS and it works.
PhantomJS
Thanks everyone!
回答3:
WebElement checkBox = driver.findElement(By.xpath("//*[@id='chkConfirm']"))
checkBox.isDisplayed();
if(!checkBox.isSelected())
checkBox.click();
Try this block.
来源:https://stackoverflow.com/questions/44896825/selenium-htmlunitdriver-clicking-on-checkbox