Selenium HtmlUnitDriver clicking on checkbox

扶醉桌前 提交于 2019-12-12 04:57:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!