【问题标题】:unable to switch back to the parent frame using selenium web driver-java?无法使用 selenium web driver-java 切换回父框架?
【发布时间】:2015-02-23 14:53:14
【问题描述】:

driver.switchTo().frame(driver.findElement(By.xpath("/html/body/form/div[12]/div[1]/div[5]/iframe")));

    new Select(driver.findElement(By.id("ddlFileExtensions"))).selectByVisibleText("DLA-Extension");              
    driver.findElement(By.cssSelector("option[value=\"DLA-Extension\"]")).click();
    driver.findElement(By.xpath("/html/body/form/div[3]/fieldset/table/tbody/tr[3]/td[2]/div[1]/input[1]")).sendKeys("TestDLA");

    driver.findElement(By.xpath("/html/body/form/div[3]/fieldset/table/tbody/tr[4]/td[3]/a/input[1]")).click();      

    //Switch to new window opened
      for(String winHandle : driver.getWindowHandles()){
          driver.switchTo().window(winHandle);
      }
      driver.findElement(By.xpath("/html/body/form/div[3]/div[3]/div/table[1]/tbody/tr[1]/td[3]/a")).click();
      driver.findElement(By.xpath("/html/body/form/div[3]/div[3]/div/table[10]/tbody/tr[2]/td[3]/a")).click();
      driver.findElement(By.xpath("/html/body/form/div[3]/div[4]/a[1]/input[1]")).click();    




      driver.switchTo().frame(driver.findElement(By.xpath("/html/body/form/div[12]/div[1]/div[5]/iframe")));
      //driver.findElement(By.xpath("/html/body/form/div[3]/div/a[1]/input[1]")).click();
      mouseClickByLocation("/html/body/form/div[3]/div/a[1]/input[1]");

【问题讨论】:

  • 请尝试上传竞争代码,它将显示您如何存储父窗口句柄并调用它。您上传的代码未涵盖主要部分,可能是投反对票的原因。

标签: java selenium


【解决方案1】:

我的答案与您的代码不符。但我正在向您展示如何实现这一目标。只需确保将所有步骤正确放置在代码中即可。

Step1:存储主窗口句柄

String Parentwindow = driver.getWindowHandle( );  

第二步:点击链接,打开新的浏览器窗口

第三步:切换到新打开的浏览器窗口。

for ( String currentwindow : driver.getWindowHandles())  
  driver.switchTo( ).window(currentwindow);  
       {  
       // Perform your operations on the current window    
         driver.close( )  
       }  

第四步:返回主窗口

driver.switchTo().window(Parentwindow);  

【讨论】:

    猜你喜欢
    • 2012-12-31
    • 2017-12-13
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多