使用selenium在子窗口中选择值后无法移动到父窗口

时间:2014-02-24 13:59:41

标签: selenium

我使用下面的代码将窗口切换到子窗口并选择一个单选按钮并切换回父窗口以选择几个值,但是在子窗口中选择一个值后,它不允许我选择一个父窗口中的值

它说“无法找到元素”

fd.findElementByXPath("//*[@id='image22']").click();
String parentwindowRMU=fd.getWindowHandle();
Set<String> handles1=fd.getWindowHandles();
for(String Windowhandles1:handles1)
{
   if(!Windowhandles1.equals(parentwindowRMU))
   {
       fd.switchTo().window(Windowhandles1);   
       Thread.sleep(7000);
       fd.findElementByXPath("//*[@id='radio1']").click();

       fd.findElementByXPath("//*[@id='ext-gen31']").click();
       Thread.sleep(5000);
       break;                       
   }            
}
fd.switchTo().window(parentwindowRMU);

3 个答案:

答案 0 :(得分:0)

我使用类似的代码:

  1. 点击“新建”按钮
    1. 将打开另一个窗口以填写表格
    2. 点击完成按钮
    3. 现在点击上一个窗口中的“提交”按钮
    4. 退出应用程序
  2. 以下是我的代码:

      String parentWindow = driver.getWindowHandle();
            Set<String> handles =  driver.getWindowHandles();
               for(String windowHandle  : handles)
                   {
                   if(!windowHandle.equals(parentWindow))
                      {
                      driver.switchTo().window(windowHandle);
                      driver.findElement(By.id("student_fname1")).clear();
                    driver.findElement(By.id("student_fname1")).sendKeys("test");
                    driver.findElement(By.id("student_lname1")).clear();
                    driver.findElement(By.id("student_lname1")).sendKeys("auto");
                    driver.findElement(By.id("student_username1")).clear();
                  driver.findElement(By.id("student_username1")).sendKeys("testuser5");
                    driver.findElement(By.id("student_password1")).clear();
                   driver.findElement(By.id("student_password1")).sendKeys("password");
                    new Select(driver.findElement(By.id("student_grade1"))).selectByVisibleText("testvalue");
                    driver.findElement(By.name("submit_done")).click();
    
                  driver.switchTo().window(parentWindow); //cntrl to parent window
    
                     driver.findElement(By.className("frm_btn")).click();//button on parent window
                     driver.findElement(By.linkText("Logout")).click();//button on parent window
                      }
                   }
    

    使用以下代码并查看其是否有效

    fd.findElementByXPath("//*[@id='image22']").click();
        String parentwindowRMU=fd.getWindowHandle();
        Set<String> handles1=fd.getWindowHandles();
        for(String Windowhandles1:handles1)
        {
           if(!Windowhandles1.equals(parentwindowRMU))
           {
               fd.switchTo().window(Windowhandles1);   
               Thread.sleep(7000);
               fd.findElementByXPath("//*[@id='radio1']").click();
        fd.close();
               fd.findElementByXPath("//*[@id='ext-gen31']").click();
               Thread.sleep(5000);
    
              fd.switchTo().window(parentwindowRMU);
            //code to perform on parent window.         
           }            
        }
    

    根据我的理解你的代码很好,问题可能是xpath未正确用于在父窗口上定位元素。

答案 1 :(得分:0)

使用迭代器和while循环来存储各种窗口句柄,然后来回切换。

//Click your link
    driver.findElement(By.xpath("xpath")).click();
    //Get all the window handles in a set
    Set <String> handles =driver.getWindowHandles();
    Iterator<String> it = handles.iterator();
    //iterate through your windows
    while (it.hasNext()){
    String parent = it.next();
    String newwin = it.next();
    driver.switchTo().window(newwin);
    //perform actions on new window
    driver.close();
    driver.switchTo().window(parent);
                        }

答案 2 :(得分:0)

您可以使用Iterator。在父窗口和子窗口之间切换的示例示例:

    WebDriver driver = new FirefoxDriver();
    driver.get("https://www.aplha.co.in/");
    driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);

    driver.findElement(By.linkText("alpha")).click();
    Set<String> windowHandlesAfter = driver.getWindowHandles();
    Iterator<String> itAfter = windowHandlesAfter.iterator();

    String parent = itAfter.next();
    String child = itAfter.next();     
    driver.switchTo().window(child);

    driver.findElement(By.xpath("//img[@alt='Ginger Hotel']")).click();
    System.out.println("After clicking on Ginger Hotels on child window");


    Set<String> windowHandlesAfter1 = driver.getWindowHandles();
    Iterator<String> itAfter1 = windowHandlesAfter1.iterator();

    System.out.println("Printing windowHandles" + driver.getWindowHandles()); 

    parent = itAfter1.next();
    child  = itAfter1.next();
    String child1 = itAfter1.next();

   driver.switchTo().window(child1);