【问题标题】:Trying to Run the below program尝试运行以下程序
【发布时间】:2015-06-30 20:50:17
【问题描述】:
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)

【问题讨论】:

  • 请仔细阅读您的异常 findElement(By.linkText(...)) 不起作用
  • 在我将其更改为链接之前它是 findElement(By.name()),因为它当时也不起作用。现在已经解决了,谢谢。

标签: java google-chrome selenium


【解决方案1】:

尝试以下,它工作正常....

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();

}

}

【讨论】:

  • 你能告诉我什么时候使用cssSelector、xpath和其他常用定位器吗?
  • 以上答案为你所用,请采纳为正确答案,其他人可以参考此答案
  • 这不是我们应该使用 Xpath 或 cssSelector 的限制。但是在 IE 浏览器中,相比 Xpath,CSS 选择器要快得多。
猜你喜欢
  • 1970-01-01
  • 2020-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-18
  • 2017-02-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多