试图运行以下程序

时间:2015-04-22 22:52:49

标签: java google-chrome selenium

import java.util.Set;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class PracticeSwitchWindow {

        public static WebDriver driver;

        public static void main(String[] args) {
            System.setProperty("webdriver.chrome.driver","C:\\Users\\Sneha\\Downloads\\chromedriver_win32\\chromedriver.exe");

            // Create a new instance of the Chrome driver

            driver = new ChromeDriver();

            // Put an Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception

            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

            // Launch the URL

            driver.get("http://www.toolsqa.com/automation-practice-switch-windows/");

            // Store and Print the name of the First window on the console

            String handle= driver.getWindowHandle();

            System.out.println(handle);

            // Click on the Button "New Message Window"

            driver.findElement(By.linkText("New Message Window")).click();

            // Store and Print the name of all the windows open               

            Set <String> handles = driver.getWindowHandles();

            System.out.println(handles);

            // Pass a window handle to the other window

            for (String handle1 : driver.getWindowHandles()) {

                System.out.println(handle1);

                driver.switchTo().window(handle1);

                }

            // Closing Pop Up window

            driver.close();

            // Close Original window

            driver.quit();

    }

}

我收到错误

Starting ChromeDriver 2.15.322448 (52179c1b310fec1797c81ea9a20326839860b7d3) on port 13328
Only local connections are allowed.
CDwindow-36C2A5E8-B9F3-4F43-9C7E-C94A9EB173A9
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element
  (Session info: chrome=42.0.2311.90)
  (Driver info: chromedriver=2.15.322448 (52179c1b310fec1797c81ea9a20326839860b7d3),platform=Windows NT 6.3 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 10.12 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.44.0', revision: '76d78cf', time: '2014-10-23 20:03:00'
System info: host: 'snehagoutam', ip: '192.168.1.3', os.name: 'Windows 8.1', os.arch: 'x86', os.version: '6.3', java.version: '1.7.0_67'
Session ID: 3acadcc58fe7c7524ade07cb0ccfcdf9
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{platform=WIN8_1, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={userDataDir=C:\Users\Sneha\AppData\Local\Temp\scoped_dir4576_1324}, rotatable=false, locationContextEnabled=true, mobileEmulationEnabled=false, version=42.0.2311.90, takesHeapSnapshot=true, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=true, browserConnectionEnabled=false, webStorageEnabled=true, nativeEvents=true, applicationCacheEnabled=false, takesScreenshot=true}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:352)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByLinkText(RemoteWebDriver.java:401)
    at org.openqa.selenium.By$ByLinkText.findElement(By.java:242)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:344)
    at Selenium.PracticeSwitchWindow.main(PracticeSwitchWindow.java:40)

1 个答案:

答案 0 :(得分:0)

Try the following, it is working fine....

import java.util.Set;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;


public class PracticeSwitchWindow {

 public static WebDriver driver;

 public static void main(String[] args) {        
    // System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+"\\chromedriver\\chromedriver.exe");
    System.setProperty("webdriver.chrome.driver","C:\\Users\\Sneha\\Downloads\\chromedriver_win32\\chromedriver.exe");
    // Create a new instance of the Chrome driver
    driver = new ChromeDriver();
    // Put an Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    // Launch the URL
    driver.get("http://www.toolsqa.com/automation-practice-switch-windows/");
    // Store and Print the name of the First window on the console
    String handle= driver.getWindowHandle();
    System.out.println(handle);
    // Click on the Button "New Message Window"
    driver.findElement(By.xpath("//button[@onclick='newMsgWin()']")).click();
    // driver.findElement(By.linkText("New Message Window")).click();
    // Store and Print the name of all the windows open               
    Set <String> handles = driver.getWindowHandles();
    System.out.println(handles);
    // Pass a window handle to the other window
    for (String handle1 : driver.getWindowHandles()) {
         System.out.println(handle1);
         driver.switchTo().window(handle1);
         }        
    driver.close();
    // Close Original window
    driver.quit();

}

}