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

时间:2018-02-20 08:52:04

标签: java eclipse selenium

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工作表中输入字段。在Web上的搜索字段中,它显示了一些自动建议,我想从列表中获取匹配结果的索引并将其打印在控制台中。

在上面的代码if(e.getText().toString().equals(str))中,此行未执行或因此索引未打印。

请帮忙!

2 个答案:

答案 0 :(得分:0)

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

试试这个:

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

答案 1 :(得分:0)

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

我得到以下输出:

尼赫鲁广场

新德里,德里

0 =尼赫鲁地方指数

新德里爱神酒店

Nehru Place,新德里,德里

1 =尼赫鲁地方指数

S2 Nehru Place,新德里,德里

2 =尼赫鲁地方指数

902 Nehru Place,新德里,德里

3 =尼赫鲁地方指数

901 Nehru Place,新德里,德里

4 =尼赫鲁地方指数

903 Nehru Place,新德里,德里

5 =尼赫鲁地方指数

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

6 =尼赫鲁地方指数

尼赫鲁广场 Karnal,Karnal District Haryana

7 =尼赫鲁地方指数

rrd.net.in

相关问题