org.openqa.selenium.ElementNotVisibleException:切换到第二个tiime的同一弹出窗口后出现的元素不可见

时间:2014-09-04 20:48:34

标签: selenium-webdriver nosuchelementexception

我正在尝试在Lufthamsa Airwyas网站 - 网址http://www.lufthansa.com/online/portal/lh/ua/homepage中自动化SearchFlights UseCase。 单击“从”链接后,将打开一个弹出窗口,我们必须从下拉列表中选择原始国家/地区,根据国家/地区vaklue,将加载城市下拉列表,选择城市后,将在第三个下拉列表中填充相应的机场。然后必须将焦点移动到主窗口并且必须单击To链接,这再次导致相同的弹出窗口(具有针对目的地县的>>城市>>机场的相同的投影)。这是我收到错误的地方。好像WbDriver再次切换到相同的poopup,但它无法在页面上找到元素。这是第一次真的令人沮丧。我的代码如下 -

公共类SearchFlights {

public static void main(String[] args) throws InterruptedException {

    System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("http://www.lufthansa.com/online/portal/lh/ua/homepage");
    Thread.sleep(3000L);
    String main_window = driver.getWindowHandle();
    System.out.println("Main Window Handle: "+driver.getWindowHandle());
    Thread.sleep(3000L);
    driver.findElement(By.xpath("//a[@href='#']/span[@class='airport-dir']")).click();
    Thread.sleep(3000L);
    for(String sub1_window : driver.getWindowHandles()){
        driver.switchTo().window(sub1_window);
        driver.findElement(By.xpath("//div[@class='cl first']/ul[@class='aa-select aa-  country']/li[@data-value='IN']")).click();
        Thread.sleep(2000L);
        driver.findElement(By.xpath("//div[@class='cl']/ul[@class='aa-select aa-city']/li[@data-value='DEL']")).click();
        Thread.sleep(2000L);
        driver.findElement(By.xpath("//div[@class='cl last']/ul[@class='aa-select aa-airport']/li[@data-value='DEL']")).click();
        Thread.sleep(2000L);
        driver.switchTo().window(main_window);
        Thread.sleep(2000L);
        driver.findElement(By.xpath("//a[@href='#'][@id ='aa-destination']/span[@class='airport-dir']")).click();
         Thread.sleep(2000L);
        driver.switchTo().window(sub1_window);
            Thread.sleep(3000L);
            System.out.println("Sub Window2 Handle: ");
            driver.findElement(By.xpath("//div[@class='cl first']/ul[@class='aa-select aa-country']/li[@data-value='HK']")).click();
            Thread.sleep(3000L);
            driver.findElement(By.xpath("//div[@class='cl']/ul[@class='aa-select aa-city']/li[@data-value='IEV']")).click();
            Thread.sleep(3000L);
            //driver.findElement(By.xpath("//a[@title='Select'][@href='#']/span")).click();
            driver.findElement(By.xpath("//div[@class='cl last']/ul[@class='aa-select aa-airport']/li[@data-value='KBP']")).click();
            Thread.sleep(3000L);*/
    }
     driver.switchTo().window(main_window);*/
}

1 个答案:

答案 0 :(得分:0)

您尝试处理的元素不在另一个窗口中,它位于同一窗口中。 试试吧!

public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.lufthansa.com/online/portal/lh/ua/homepage");
String main_window = driver.getWindowHandle();
WebElement from = driver.findElement(By.id("aa-origin"));
from.click();
Thread.sleep(3000L);
driver.findElement(By.xpath("//div[@class='cl first']/ul[@class='aa-select aa-country']/li[@data-value='IN']")).click();
 Thread.sleep(2000L);
 driver.findElement(By.xpath("//div[@class='cl']/ul[@class='aa-select aa-city']/li[@data-value='DEL']")).click();
 Thread.sleep(2000L);
 driver.findElement(By.xpath("//div[@class='cl last']/ul[@class='aa-select aa-airport']/li[@data-value='DEL']")).click();
 Thread.sleep(2000L);
 WebElement to = driver.findElement(By.id("aa-destination"));
 to.click();
 Thread.sleep(3000L);
 driver.findElement(By.xpath(".//*[@id='dijit_Dialog_1']/div[2]/div/div[2]/div[1]/ul/li[@data-value='HK']")).click();
 Thread.sleep(3000L);
 driver.findElement(By.xpath(".//*[@id='dijit_Dialog_1']/div[2]/div/div[2]/div[3]/ul/li[@data-value='HKG']")).click();
 Thread.sleep(3000L);
 driver.findElement(By.xpath(".//*[@id='dijit_Dialog_1']/div[2]/div/div[2]/div[5]/ul/li[@data-value='HKG']")).click();
 Thread.sleep(3000L);}