简体   繁体   English

在带有 Java 的 Selenium 中,如何取消选中某些复选框?

[英]In Selenium with Java, how to uncheck some checkboxes?

How can I write in Selenium with java the code to uncheck one of these checkboxes?如何在 Selenium 中使用 java 编写代码以取消选中这些复选框之一? In the original search are displayed checked but I want to uncheck one or two or all three to see less results, this is the displayed search:在原始搜索中显示选中但我想取消选中一个或两个或所有三个以查看更少的结果,这是显示的搜索:

Filter results by provider: [x]Facebook (25911), [x]Hotmail (7651), [x]Yahoo (11)按提供商过滤结果:[x]Facebook (25911)、[x]Hotmail (7651)、[x]Yahoo (11)

Thank you for your help :)感谢您的帮助 :)

A colleague helped me and it was very easy, here is the way it worked:一位同事帮助了我,这很容易,这是它的工作方式:

wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("custom-checkbox[label^='Yahoo']"))).click();

Thank you for your help.感谢您的帮助。 :) :)

Some elements can be really fussy.有些元素可能真的很挑剔。

Try using .sendKeys(Keys.Return) rather than .click().尝试使用 .sendKeys(Keys.Return) 而不是 .click()。

Get the list of filter result in List (list of Facebook (25911) , Hotmail(7651) and yahoo, Let say list B ).获取 List 中的过滤结果列表(Facebook (25911)、Hotmail(7651) 和 yahoo 的列表,比如列表B )。

Now iterate for loop on that list of webelements( B ) and if that element is the same that you want to uncheck then you can have a index of that element(Let say you want to uncheck Hotmail so you will get index 1 ) now just using that index get a element using that index in xpath (Ex:- //ul[@ag-id='gallery1']/li["(+i+1)+"]/div/div/h4/a).现在在 webelements( B ) 列表上迭代 for 循环,如果该元素与您要取消选中的元素相同,那么您可以拥有该元素的索引(假设您要取消选中 Hotmail,因此您将获得索引1 )现在只是使用该索引获取在 xpath 中使用该索引的元素(例如:- //ul[@ag-id='gallery1']/li["(+i+1)+"]/div/div/h4/a) . and click on that element.并单击该元素。

I see this is an old question but I recently ran into this myself.我看到这是一个老问题,但我最近自己遇到了这个问题。 I did not need to know ahead of time if the box was checked, I just wanted to force it unchecked.我不需要提前知道是否选中了该框,我只是想强制取消选中它。 The solution I came up with (using Javascript in Node.js) is similar to what I use to call setAttribute:我想出的解决方案(在 Node.js 中使用 Javascript)类似于我用来调用 setAttribute 的方法:

let element = await driver.findElement(By.id("checkBox1"));
if (element != null) {
  await driver.executeScript("arguments[0].checked = false;", element);
}

This forces the box to be unchecked regardless of whether or not it was first checked.这会强制取消选中该框,无论它是否首先被选中。 No clicks required.无需点击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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