程序在一台计算机上运行,​​但在另一台计

时间:2017-05-05 16:20:57

标签: java selenium xpath driver

使用我的java selenium项目时,我有这个非常奇怪的问题。当我在工作时,我编写并运行我编写的代码没有任何问题,但是当我回到家,编写代码,并尝试在Eclipse中编译它时它给了我:

Exception in thread "main" org.openqa.selenium.NoSuchWindowException: Unable to find element on closed window (WARNING: The server did not provide any stacktrace information)

当我将代码从家里带到我的工作电脑时,它会编译并运行。所以我知道这不是问题所在。

问题:我怎样才能让这个程序在家里的电脑上运行?这就是我正在使用的驱动程序(IE)。它崩溃的部分是我在任何方法甚至在main中调用xPath时。我会非常感谢你的帮助。

public static WebDriver driver(){
     System.setProperty("webdriver.ie.driver", "D:/Selenium/IEDriverServer.exe");

     DesiredCapabilities dc = DesiredCapabilities.internetExplorer();

     dc.setCapability("EnableNativeEvents", false);          //Disable native events

     dc.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);  // Issue with downloading the report

     dc.setCapability("ignoreZoomSetting",true);

     WebDriver thing = new InternetExplorerDriver(dc);

     thing.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

     thing.findElement(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL, "0"));  //Set zoom level to 100%

     return thing;
}

1 个答案:

答案 0 :(得分:0)

有关解决方案的一些基本信息:

  1. 要执行Selenium / Java代码,您需要将代码放在public static void main(String[] args) {}或使用TestNG,您需要将代码放在public void test1() {}@Test注释之类的方法中。你的代码中都缺少这两个。
  2. 此行public static WebDriver driver没有使用,因为您已经WebDriver thing
  3. public static void main(String[] args) {}public void test1() {}不会返回任何内容,因此请忽略return thing;
  4. 如果您的缩放设置未设置为dc.setCapability("ignoreZoomSetting",true);向前移动,则您可能会忽略缩放设置100%,您可能会遇到意外错误&异常。
  5. 设置缩放级别超出了Selinium的范围,因此您的第thing.findElement(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL, "0"));行永远不会被执行。
  6. 以下是您的工作代码,并使用public static void main(String[] args) {}为您提供一些简单的调整:

    public static void main(String[] args) {
         System.setProperty("webdriver.ie.driver", "C:/Utility/BrowserDrivers/IEDriverServer.exe");
         DesiredCapabilities dc = DesiredCapabilities.internetExplorer();
         dc.setCapability("EnableNativeEvents", false);          //Disable native events
         dc.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);  // Issue with downloading the report
         dc.setCapability("ignoreZoomSetting",true);
         WebDriver thing = new InternetExplorerDriver(dc);
         thing.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    }
    
  7. 如果这回答你的问题,请告诉我。

相关问题