简体   繁体   中英

How to click a checkbox in Java using PhantomJS and Selenium?

I want select a simple checkbox in Java using PhantomJS with Selenuim.

I did it with Firefox with this code

driver.findElement(By.xpath("//label[@for='terms-checkbox']")).click();

But when I changed to PhantomJS driver I got an error : invisible element ...

The Core problem in my case is on the xpath locator that i was using, in phantomjs if you want to click on the element which have a child div, you need to click on the specific child div of the element

<div class="ui-helper-hidden-accessible">
  <input id="addressUpdate:firmflg_input" name="addressUpdate:firmflg_input" autocomplete="off" aria-labelledby="addressUpdate:j_idt159" aria-checked="false" type="checkbox">
</div>
<div class="ui-chkbox-box ui-widget ui-corner-all ui-state-default"><span class="ui-chkbox-icon ui-icon ui-icon-blank ui-c"></span>
</div>

if you use this xpath //div[@id='addressUpdate:firmflg'] for the above code snipet it will fail to click on the checkbox if you running on phantomjs but it works in other browsers. instead if you go for //div[@id='addressUpdate:firmflg']/div[2] this will work in all browsers including phantomjs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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