嗨,大家好,所以我是Selenium / Java / HTML的相对新手(阅读:完整),所以如果我提出明显要求,我深表歉意。
我需要的是能够


单击特定复选框1,然后
如果选中了特定复选框2,则取消选中它


这是网站HTML:

特定复选框1

<div class="checkbox">
    <label id="agree_to_terms_label" for="agree_to_terms_join" class="visible">
      <input id="agree_to_terms_join" name="agree_to_terms" type="checkbox" data-required="true" data-required-message="You need to agree to the *** Account Holder agreement" data-change="" class="parsley-validated">
      <span class="left-block"></span>
      I have read, understand and agree to the <a href="/terms-and-conditions/" target="_blank">*** Account Holder Agreement</a>
      and acknowledge <a href="/privacy-policy" target="_blank">*** Privacy Policy</a>
      <input type="hidden" name="agree_to_terms" value="yes">
    </label>
  </div>


特定复选框2:

<div class="checkbox">
    <label id="agree_to_offers_label" for="agree_to_offers" class="visible">
      <span class="left-block">
         <input id="agree_to_offers" name="agree_to_offers" type="checkbox" data-required-message="" data-change="" checked="checked" value="yes">
        <span>By joining *** you'll be notified of exclusive offers and account updates via email</span>
      </span>
    </label>
  </div>


我无果的尝试:

Xpath:

driver.findElement(By.xpath("//input[@id='agree_to_terms_join' and @type='checkbox']")).click();


元素不可见

driver.findElement(By.xpath("//*[@id='agree_to_terms_join']/parent::label")).click();


点击div中的href超链接

driver.findElement(By.xpath("//*[@id='agree_to_terms_label']/input")).click();


元素不可见

CSS:

driver.findElement(By.cssSelector("input[id = 'agree_to_terms_join'][type = 'checkbox']")).click();


元素不可见

by.className:

driver.findElement(By.className("checkbox")).click();


打开超链接

我在论坛上四处张望,看到提到了隐藏的元素-但是我找不到任何iframe或其他任何似乎隐藏了bugger的东西吗?

任何帮助将不胜感激!!

最佳答案

尝试以下操作:

WebElement yourChkBox  = driver.findElement(By.xpath("//*[@id='agree_to_terms_join']/parent::label"));

WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOf(yourChkBox));

Actions act = new Actions(driver);
act.moveToElement(yourChkBox).click().build().perform();


更新:

或尝试使用javascriptexecutor,如下所示:

WebElement yourChkBox  = driver.findElement(By.xpath("//*[@id='agree_to_terms_join']/parent::label"));

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('value', 'Yes');", yourChkBox  );

关于java - Selenium 无法单击特定复选框,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46253566/

10-10 18:08