Java 丢弃循环中的某些类值-避免使用第二个元素
我正在循环使用日期选择器,但不希望返回所有值 如果“已禁用”在类中,我不想选择它 这是我目前正在使用的循环Java 丢弃循环中的某些类值-避免使用第二个元素,java,selenium,selenium-webdriver,webdriver,Java,Selenium,Selenium Webdriver,Webdriver,我正在循环使用日期选择器,但不希望返回所有值 如果“已禁用”在类中,我不想选择它 这是我目前正在使用的循环 List<WebElement> days = driver.findElements(By.className("c-datepicker-calendar__day")); for(int i1=0; i1<days.size(); i1++) { System.out.println(days.get(i1).get
List<WebElement> days = driver.findElements(By.className("c-datepicker-calendar__day"));
for(int i1=0; i1<days.size(); i1++) {
System.out.println(days.get(i1).getText());
}
List days=driver.findElements(按.className(“c-datepicker-calendar\uuuuu day”);
对于(int i1=0;i1请尝试使用以下代码捕捉:
List<WebElement> days = driver.findElements(By.className("c-datepicker-calendar__day"));
for (WebElement elem : days) {
String fullClassString = elem.getAttribute("class");
if(!fullClassString.contains("is-disabled")) {
System.out.println(elem.getText());
}
}
List days=driver.findElements(按.className(“c-datepicker-calendar\uuuuu day”);
for(WebElement元素:天){
String fullClassString=elem.getAttribute(“类”);
如果(!fullClassString.contains(“已禁用”)){
System.out.println(elem.getText());
}
}
尝试使用以下代码捕捉:
List<WebElement> days = driver.findElements(By.className("c-datepicker-calendar__day"));
for (WebElement elem : days) {
String fullClassString = elem.getAttribute("class");
if(!fullClassString.contains("is-disabled")) {
System.out.println(elem.getText());
}
}
List days=driver.findElements(按.className(“c-datepicker-calendar\uuuuu day”);
for(WebElement元素:天){
String fullClassString=elem.getAttribute(“类”);
如果(!fullClassString.contains(“已禁用”)){
System.out.println(elem.getText());
}
}
您可以使用流并对其进行过滤
List<WebElement> filteredDays = days.stream()
.filter(day -> !day.getAttribute("class").contains("is-disabled"))
.collect(Collectors.toList());
List filteredDays=days.stream()
.filter(day->!day.getAttribute(“类”).contains(“已禁用”))
.collect(Collectors.toList());
您可以使用流并对其进行过滤
List<WebElement> filteredDays = days.stream()
.filter(day -> !day.getAttribute("class").contains("is-disabled"))
.collect(Collectors.toList());
List filteredDays=days.stream()
.filter(day->!day.getAttribute(“类”).contains(“已禁用”))
.collect(Collectors.toList());
只需使用一行即可获得。无需检查使用if条件
List<WebElement> days = driver.findElements(By.cssSelector(".c-datepicker-calendar__day:not(.is-disabled)"));
List days=driver.findElements(By.cssSelector(“.c-datepicker-calendar\uuu day:not(.is disabled)”);
请尝试使用此选择器,看看是否可以仅选择“开”,而不选择“不开”。已禁用。您只需使用一行即可获得它。无需检查“使用如果”条件
List<WebElement> days = driver.findElements(By.cssSelector(".c-datepicker-calendar__day:not(.is-disabled)"));
List days=driver.findElements(By.cssSelector(“.c-datepicker-calendar\uuu day:not(.is disabled)”);
试试看你是否可以使用此选择器只选择“开”,而不选择“不开”。已禁用。@MarkH Hey bro!你能告诉我我的答案有什么问题吗?谢谢!@MarkH Hey bro!你能告诉我我的答案有什么问题吗?谢谢!