Selenium WebDriver Loop无效

时间:2016-08-21 07:48:04

标签: java loops selenium webdriver

我正在尝试打开naukri.com中的所有标签但在到达“公司”标签后却失败了。 我无法访问“服务”标签,而且还没有运行。 请帮我解释一下这段代码。

Jeny Mishra

    driver.get("http://www.naukri.com");
    List<WebElement> labels =driver.findElements(By.className("mTxt"));
    System.out.println(labels.size());
//System.out.println(labels.indexOf(1));//get the name of labels present in the webpage
    for(WebElement we : labels){
        System.out.println(we.getText());
        }
    //for(int i=0;i<=labels.size();i++){//get the name of labels present in the webpage
        //System.out.println(labels.get(i).getText());
    //}
    WebElement label1= driver.findElement(By.xpath("html/body/div[2]/div/ul/li[1]/a/div"));
    Actions action = new Actions(driver);
    action.moveToElement(label1).perform();
    Thread.sleep(3000);
Iterator<WebElement> itr=labels.iterator();
WebElement currentlabel = null;

//if(currentlabel.equals(companies)){
    //action.moveToElement(companies);
    //driver.close();
//}
    /* String homepage=driver.getWindowHandle();
     System.out.println(homepage);
     System.out.println(driver.getTitle());
     Set<String> allwindows= driver.getWindowHandles();
    System.out.println(allwindows.size());
    Iterator<String> itri=allwindows.iterator();
    String currentwindow;
    while(itri.hasNext()){
        currentwindow=itri.next().toString();
        System.out.println(currentwindow);
        if(!currentwindow.equals(homepage)){
            driver.switchTo().window(currentwindow);
            driver.close();
            Thread.sleep(3000);
        }*/
while(itr.hasNext()){
    currentlabel=itr.next();

    if(!currentlabel.equals(label1)){
    action.moveToElement(currentlabel).build().perform();

    Thread.sleep(5000);

    WebElement companies= driver.findElement(By.xpath("html/body/div[2]/div/ul/li[3]/a/div"));
    Thread.sleep(3000);
companies.sendKeys(Keys.CONTROL+"w");
driver.navigate().back();

    WebElement label3= driver.findElement(By.xpath("html/body/div[2]/div/ul/li[4]/a/div"));
    if(!currentlabel.equals(label3)){
        action.moveToElement(currentlabel).build().perform();
        Thread.sleep(5000);
    }
    //action.moveToElement(label3).build().perform();
    //Thread.sleep(5000);
    driver.close();
    }
        //System.out.println(labels.);
    }

}

}

1 个答案:

答案 0 :(得分:0)

我假设您尝试执行鼠标悬停,然后尝试单击所有主题菜单。如果是,我只是稍微调整了你的代码。请尽量避免使用Thread.sleep命令。此功能仅在Chrome浏览器中进行了测试。

    driver.get("http://www.naukri.com");

    List<WebElement> topIconLabels = driver.findElements(By.className("mTxt"));
    System.out.println(topIconLabels.size());

    for (WebElement labels : topIconLabels) {
        System.out.println(labels.getText());
    }

    Actions action = new Actions(driver);
    Iterator<WebElement> itr = topIconLabels.iterator();
    WebElement currentlabel = null;
    while (itr.hasNext()) {
        currentlabel = itr.next();
        action.moveToElement(currentlabel).build().perform();
        currentlabel.click();
        Thread.sleep(5000);
        // when login label is clicked, it doesn't open in a new tab
        if (currentlabel.getText().equalsIgnoreCase("login")) {
            driver.findElement(By.id("loginLB")).findElement(By.id("closeLB")).click();
            Thread.sleep(2000);
        } else { // closing the newly opened tabs
            Robot robot = new Robot();
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_W);
            Thread.sleep(2000);
        }
    }
    driver.quit();