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.);
}
}
}
你的代碼真的很難理解。可以清理並重新發布。 – Grasshopper