在控制台上打印特定搜索结果的索引


driver.navigate().to("https://maps.mapmyindia.com/");
Thread.sleep(3000);
driver.findElement(By.xpath(".//*[@id='onboarding-popup-sec']/div/div/button")).click();
File src = new File("D:\Screenshots\CityList.xlsx");
FileInputStream fis = new FileInputStream(src);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet1 = wb.getSheetAt(0);
int rowcount = sheet1.getLastRowNum();
for(int i=1; i<=rowcount; i++) {
    String str = sheet1.getRow(i).getCell(0).getStringCellValue();
    fis.close();
    driver.findElement(By.xpath(".//*[@id='auto']")).clear();
    driver.findElement(By.xpath(".//*[@id='auto']")).sendKeys(str);
    Thread.sleep(5000);
    By mySelector = By.xpath(".//*[@id='asa']/div[2]/span");
    List<WebElement> elements = driver.findElements(mySelector);
    int count=0;
    for(WebElement e: elements) {
        count++;
        System.out.println(e.getText());
        if(e.getText().toString().equals(str)) {
            System.out.println(count);
            sheet1.getRow(i).createCell(1).setCellValue("Pass");
        }
        else {
            sheet1.getRow(i).createCell(1).setCellValue("Fail");
        }
    }
    FileOutputStream fos = new FileOutputStream(src);
    wb.write(fos);
    fos.close();
}

在此代码中,我正在打开Chrome并通过Selenium导航到地图站点,并从Excel表中输入一个字段。在网络上的搜索字段中,它显示了一些自动建议,我想从列表中获取匹配结果的索引并将其打印在控制台中。

在上面的代码if(e.getText().toString().equals(str))中,此行不执行或其他内容,因此索引没有打印。

请帮助!

List<WebElement> list = div.findElements(By.tagName("li"));
Thread.sleep(5000);
int listsize= list.size();
for(int i=0;i<listsize;i++)
 {
      Thread.sleep(1000);
      System.out.println(list.get(i).getText());
      if(list.get(i).getText().contains("Nehru Place"))
      {
       System.out.println(i+"=index of Nehru Place");
      }
}

正在以下输出:

Nehru Place

新德里,德里

0 = nehru place的索引

EROS Hotel New Delhi

Nehru Place,新德里,德里

1 = nehru plot的索引

S2德里新德里的尼赫鲁广场

2 = nehru plot的索引

902德里新德里的尼赫鲁广场

3 = nehru plot的索引

901德里新德里的尼赫鲁广场

4 = nehru place的索引

903德里新德里的尼赫鲁广场

5 = nehru place的索引

Nehru Place Epicuria26,德里新德里的尼赫鲁广场,德里 - 110065

6 = nehru place的索引

Nehru Place卡尔纳尔(Karnal(,哈里亚纳邦(Karnal District(

7 = nehru place的索引

rrd.net.in

下面代码与您从搜索建议中搜索的精确字符串匹配。

尝试以下操作:

    driver.get("https://maps.mapmyindia.com/");
    Thread.sleep(3000);
    driver.findElement(By.xpath(".//*[@id='onboarding-popup-sec']/div/div/button")).click();
    Thread.sleep(2000);
    driver.findElement(By.xpath("//div[@class='input-group']//input[@id='auto']")).sendKeys(placeToSearch);
    Thread.sleep(2000);
    List<WebElement> elements = driver
            .findElements(By.xpath("//div[@class='as-results']//li[@class='as-result-item search-res']"));
    boolean match = false;
    int count = 0;
    for (WebElement element : elements) {
        count++;
        String text = element.findElement(By.xpath(".//div[@class='search-item']/span")).getAttribute("innerHTML");
        int index = text.indexOf('<');
        text = text.substring(0,index);
        if (text.equalsIgnoreCase(placeToSearch)) {
            System.out.println("Index of the element is " + count);
            match = true;
            break;
        }
    }
    if (!match) {
        System.out.println("Could not find the match in the result");
    }

最新更新