在Selenium中,无法切换到主窗口

时间:2018-03-02 09:59:04

标签: selenium selenium-webdriver

我知道我的代码中有一个小故障,你能告诉我为什么我无法切换到主窗口。

我正在尝试打开多个窗口并使用方法切换到主窗口,并且只关闭所需的窗口,如果其标题与我在代码中硬编码的字符串匹配。很抱歉发布整个代码,只是无法弄清楚我的代码中的错误位置。如果可以,请帮助我!谢谢你的阅读。

import java.util.Iterator;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class MutiWindowsnaukri {


public static void multiWind(WebDriver driver) throws InterruptedException{

        Iterator<String>it =  driver.getWindowHandles().iterator();


        while(it.hasNext()){
        System.out.println("switching to mainWindow");
        String mWind =   it.next();
        driver.switchTo().window(mWind);
        System.out.println(driver.getTitle());
        Thread.sleep(6000L);
        }

          }

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub


WebDriver driver = new ChromeDriver();

driver.get("http://www.naukri.com");
Thread.sleep(2000L);
driver.findElement(By.linkText("Post Jobs")).click();
multiWind(driver);
Thread.sleep(2000L);
driver.findElement(By.linkText("Employer Zone")).click();
multiWind(driver);
Thread.sleep(2000L);
driver.findElement(By.linkText("Employer Zone")).click();
multiWind(driver);
Thread.sleep(2000L);
driver.findElement(By.linkText("Report a Problem")).click();
multiWind(driver);
                  Set<String>s2 = driver.getWindowHandles();
       Iterator<String> it1=    s2.iterator();
       // Iterator<String> it1 = driver.getWindowHandles().iterator();
//Iterator<String>it1 =  (Iterator<String>) driver.getWindowHandles().iterator();
        String str ="Post";
while(it1.hasNext()){
driver.switchTo().window(it1.next());
String title = driver.getTitle();

if(title.equalsIgnoreCase(str)){
driver.close();
break;
}

}

}

}

错误:

switching to mainWindow
Jobs - Recruitment - Job Search - Employment - Job Vacancies - Naukri.com
switching to mainWindow
Jobs - Recruitment - Job Search - Employment - Job Vacancies - Naukri.com
switching to mainWindow
Post Jobs – Job posting Site – Post Vacancy Naukri.com
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: Employer Zone
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html

0 个答案:

没有答案