簡體   English   中英

如何使用PhantomJS和Selenium單擊Java中的復選框?

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

我想使用帶有Selenuim的PhantomJS在Java中選擇一個簡單的復選框。

我是用Firefox用此代碼完成的

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

但是,當我更改為PhantomJS驅動程序時,出現了一個錯誤:不可見元素...

在我的案例中,核心問題是我使用的xpath定位器,在phantomjs中,如果您要單擊具有子div的元素,則需要單擊該元素的特定子div。

<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>

如果對上面的代碼片段使用xpath //div[@id='addressUpdate:firmflg'] ,如果您在phantomjs上運行,它將無法單擊復選框,但在其他瀏覽器中也可以使用。 相反,如果您//div[@id='addressUpdate:firmflg']/div[2]它將在包括phantomjs在內的所有瀏覽器中運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM