简体   繁体   English

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

[英]Print index of the specific search result on the console

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();
}

In this code, I am opening Chrome and navigate to a map site through selenium and give an input of a field from an Excel sheet.在这段代码中,我打开 Chrome 并通过 selenium 导航到一个地图站点,并从 Excel 工作表中输入一个字段。 In the search field on the web, it shows some auto suggestions and I want to get the index of the matching result from the list and print it in the console.在网络上的搜索字段中,它显示了一些自动建议,我想从列表中获取匹配结果的索引并将其打印在控制台中。

In the above code if(e.getText().toString().equals(str)) this line is not executing or something so the index is not printing.在上面的代码中if(e.getText().toString().equals(str))这行没有执行或其他东西所以索引没有打印。

Please help!请帮忙!

Below code match the exactly string that you search for from the search suggestion.下面的代码与您从搜索建议中搜索的字符串完全匹配。

Try This:尝试这个:

    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");
    }
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");
      }
}

Am getting following output:我得到以下输出:

Nehru Place尼赫鲁广场

New Delhi, Delhi新德里, 德里

0=index of Nehru Place 0=尼赫鲁广场的索引

Eros Hotel New Delhi爱神酒店 新德里

Nehru Place, New Delhi, Delhi Nehru Place, 新德里, 德里

1=index of Nehru Place 1=尼赫鲁广场的索引

S2 Nehru Place, New Delhi, Delhi S2 Nehru Place, 新德里, 德里

2=index of Nehru Place 2=尼赫鲁广场的索引

902 Nehru Place, New Delhi, Delhi 902 Nehru Place, 新德里, 德里

3=index of Nehru Place 3=尼赫鲁广场的索引

901 Nehru Place, New Delhi, Delhi 901 Nehru Place, 新德里, 德里

4=index of Nehru Place 4=尼赫鲁广场的索引

903 Nehru Place, New Delhi, Delhi 903 Nehru Place, 新德里, 德里

5=index of Nehru Place 5=尼赫鲁广场的索引

Nehru Place Epicuria 26, Nehru Place, New Delhi, South District, Delhi- 110065 Nehru Place Epicuria 26, Nehru Place, New Delhi, South District, Delhi - 110065

6=index of Nehru Place 6=尼赫鲁广场的索引

Nehru Place Karnal,Karnal District Haryana Nehru Place Karnal, Karnal District 哈里亚纳邦

7=index of Nehru Place 7=尼赫鲁广场的索引

rrd.net.in rrd.net.in

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

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