窗口开关不在硒中工作

时间:2016-04-14 10:42:50

标签: selenium switch-statement window

我正在与Selenium Automation合作。在此,当我单击当前窗口中的链接时,将打开一个新窗口。我只想将控件切换到新窗口。但我无法做到这一点。实际上,新窗口是自动生成的窗口。也就是说,链接将动态生成。帮帮我朋友。

    import java.io.File;
    import java.util.Set;
    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.ie.InternetExplorerDriver;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;

    public class UAN {
        public static void main(String[] args) { 
            File file = new File("D:\\IEDriverServer.exe");
            System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
            WebDriver driver = new InternetExplorerDriver ();
            driver.get("http://intraeasy.techmahindra.com/easyhome.aspx");
            System.out.println(driver.getTitle());
            String Parent_Window = driver.getWindowHandle();   
            System.out.println("Main window value: "+Parent_Window);
            //CLick to open new window
           driver.findElement(By.linkText("People Search")).click();
           // Switching from parent window to child window   
           for (String Child_Window : driver.getWindowHandles())  
             driver.switchTo().window(Child_Window);
          {
          // Performing actions on child window  
        driver.findElement(By.xpath("/html/body/form/table/tbody/tr/td/table/tbody/tr[2]/td/table/tbody/tr[5]/td/table/tbody/tr[4]/td/table/tbody/tr[2]/td[2]/table/tbody/tr[2]/td/table/tbody/tr[1]/td[2]/input")).sendKeys("test");
       // Switching back to Parent Window
         driver.switchTo().window(Parent_Window); 
      }
    }
}

代码在执行此行时给出以下错误。

driver.findElement(By.xpath("/html/body/form/table/tbody/tr/td/table/tbody/tr[2]/td/table/tbody/tr[5]/td/table/tbody/tr[4]/td/table/tbody/tr[2]/td[2]/table/tbody/tr[2]/td/table/tbody/tr[1]/td[2]/input")).sendKeys("test");


Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with name == notetext (WARNING: The server did not provide any stacktrace information)

Command duration or timeout: 5.11 seconds

1 个答案:

答案 0 :(得分:0)

您编写的代码如下: -

for (String Child_Window : driver.getWindowHandles())  
     driver.switchTo().window(Child_Window);
  {
  // Performing actions on child window  
driver.findElement(By.xpath("/html/body/form/table/tbody/tr/td/table/tbody/tr[2]/td/table/tbody/tr[5]/td/table/tbody/tr[4]/td/table/tbody/tr[2]/td[2]/table/tbody/tr[2]/td/table/tbody/tr[1]/td[2]/input")).sendKeys("test");
  // Switching back to Parent Window
 driver.switchTo().window(Parent_Window); 
}

逻辑错误本身。您需要单独找到所有窗口,然后切换它。

Set<String> windows = driver.getWindowHandles();
Iterator<String> iterate = windows.iterator();
String parentWindow = iterate.next();
String childWindow = iterate.next();
driver.switchTo().window(childWindow);
// do your action