String[] arr = {"Month","Jan","Feb","Mar", "Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
WebElement dropDown = getDriver().findElement(By.id("month"));
Select select = new Select(dropDown);
List<WebElement> options = select.getOptions();
for(WebElement we : options){
for(int i = 1; i<arr.length; i++){
if(we.getText().equals(arr[i])){
System.out.println("Matched" );
break;
}
}
}
我想通过在Java中使用Selenium在Facebook下拉菜单中查找重复的月份。
我尝试了findelement,web元素,但是我做不到。马上;我可以将月份与网页相匹配。原始问题是这样的:查找月份下拉列表是否包含重复项。如果是,则打印出重复的值。 thnx
答案 0 :(得分:-1)
由于您的用例是通过使用 Selenium 和 Java 在Facebook下拉列表中查找重复的月份,因此您实际上不需要引用数组。要查找月份下拉列表是否包含重复项并打印出重复的值,可以使用以下解决方案:
此代码块从 Facebook 上的下拉列表中提取月的列表:
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.facebook.com");
WebElement dropDown = driver.findElement(By.id("month"));
Select select = new Select(dropDown);
List<WebElement> options = select.getOptions();
ArrayList<String> months = new ArrayList<>();
for(WebElement we : options)
months.add(we.getText());
System.out.println("Current list of months: " +months);
为了通过代码块进行示范,我们将添加3个重复的条目:
months.add("Month");
months.add("Feb");
months.add("Dec");
在此代码块中,我们创建了一个函数,用于返回重复的月份:
public static Set<String> findDuplicates(List<String> listContainingDuplicates)
{
final Set<String> setToReturn = new HashSet<>();
final Set<String> set1 = new HashSet<>();
for (String myString : listContainingDuplicates)
{
if (!set1.add(myString))
setToReturn.add(myString);
}
System.out.println("Unique months: " +set1);
return setToReturn;
}
现在,您可以按以下方式调用函数findDuplicates()
:
System.out.println("Duplicated months: " +findDuplicates(months));
因此完整的解决方案将是:
package Duplicates;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class Find_Duplicates_In_List {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.facebook.com");
WebElement dropDown = driver.findElement(By.id("month"));
Select select = new Select(dropDown);
List<WebElement> options = select.getOptions();
ArrayList<String> months = new ArrayList<>();
for(WebElement we : options)
months.add(we.getText());
System.out.println("Current list of months: " +months);
months.add("Month");
months.add("Feb");
months.add("Dec");
System.out.println("Duplicated months: " +findDuplicates(months));
}
public static Set<String> findDuplicates(List<String> listContainingDuplicates)
{
final Set<String> setToReturn = new HashSet<>();
final Set<String> set1 = new HashSet<>();
for (String myString : listContainingDuplicates)
{
if (!set1.add(myString))
setToReturn.add(myString);
}
System.out.println("Unique months: " +set1);
return setToReturn;
}
}
控制台输出:
Current list of months: [Month, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sept, Oct, Nov, Dec]
Unique months: [Jul, Oct, Feb, Apr, Jun, Dec, May, Month, Sept, Aug, Nov, Jan, Mar]
Duplicated months: [Month, Feb, Dec]