当前位置: 首页 > 知识库问答 >
问题:

硒不能单击特定复选框

鄂和璧
2023-03-14

大家好,我是Selenium/Java/HTML的相对(阅读:complete)新手,如果我问的是显而易见的问题,我深表歉意。我需要的是能够

  1. 单击特定复选框1和

以下是网站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();

元素不可见

通过类名:

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

打开超链接

我在论坛上看了看,看到提到元素被隐藏了——但是我找不到任何iframe或其他似乎隐藏了窃听器的东西?

任何帮助都将不胜感激!!

共有3个答案

欧阳勇
2023-03-14

试试这个:

WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.quidco.com/join-quidco/");          
driver.findElement(By.xpath(".//label[@id='agree_to_terms_label']")).click();         
driver.findElement(By.xpath(".//label[@id='agree_to_offers_label']")).click();

如有必要,将隐式或显式等待

程赞
2023-03-14

尝试以下内容:

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  );
田晨
2023-03-14

要单击的确切元素是“::before”,这是一个伪元素。我认为您需要使用Javascript。下面的代码应该适合您

WebElement element1 = driver.findElement(By.cssSelector(".left-block"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element1);
 类似资料:
  • 我正在使用selenium测试脚本。我得到以下错误和这个错误随机发生。当我跑10次时,我会得到大约两次。所以它不是真的可复制的。有人知道为什么会这样吗?我试图单击的元素在浏览器中绝对可见,并且不会四处移动,因此不需要调整大小或拖动元素。我正在使用chrome webdriver,我阅读了其他故障排除策略(调试“Element is not clickable at point”错误),它们似乎与我

  • 我正在学习硒,我需要一些帮助。好吧,我正在使用这个网站。这是用例: < li >在搜索栏中输入文本(完成) < li >按搜索按钮。(未完成) 我已尝试使用类名单击按钮,但它不起作用 我使用的是Chromedrive 74和selenium : 3.9.0版本

  • 我用selenium和python一起使用,我试图点击一个按钮,但它似乎不起作用。下面是html结构的图片: 这里 我已经尝试通过xpath和类单击该按钮,但没有成功。它也没有给我一个错误。我为每一个答案感到高兴!

  • 如果复选框不确定,如何从与复选框关联的标签停止单击事件? 我尝试了以下几点,但都没有成功: El是复选框本身。 目标是,当此复选框不确定时,单击相关标签后,我希望它所做的就是退出不确定并取消选中。然后,我将触发表中所有选中的复选框也被取消选中。 问题是,当我单击不确定的复选框标签时,它会导致它被选中,导致所有未选中的复选框都被选中。与我想要的相反。

  • 我用XPath写了一个单选按钮的代码,但在代码执行过程中没有点击单选按钮。 我的代码是: 请帮助如何按钮选择特定的单选按钮,选择后它移动到另一个单选按钮。 提前感谢。

  • 我有一个输入复选框被修改为按钮,一个是的,一个是的,当选中批准时,它会选中复选框等,一旦选中,第三个按钮提交。如果我在没有使用按钮的情况下选中该复选框,那么我的脚本可以正常工作,但是我会释放已经与按钮相关联的其他功能。 下面的代码基于WooCommerce order received页面中通过AJAX更新order状态 我如何监听和检测是否选中了复选框,即使它是通过选择按钮而不是实际的复选框选中