2016-08-21 91 views
1

我試圖打開naukri.com中的所有選項卡,但達到「公司」選項卡後失敗。 我無法達到「服務」選項卡,並進一步沒有運行。 請幫助我解決這個問題。Selenium WebDriver Loop不工作

耶尼·米什拉

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

你的代碼真的很難理解。可以清理並重新發布。 – Grasshopper

回答

0

我假設你正在試圖執行鼠標懸停,然後試圖點擊所有topicon菜單。如果是的話,我只是稍微調整了你的代碼。請儘量避免使用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();