【问题标题】:Program works on one computer but not another程序可在一台计算机上运行,​​但不能在另一台计算机上运行
【发布时间】:2017-05-05 16:20:57
【问题描述】:

在处理我的 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;
}

【问题讨论】:

  • 您声明每当您使用 XPath 时都会发生错误,但我在上面的代码中没有看到任何使用 XPath 的内容。通常,“NoSuchWindow”类型的错误意味着预期的浏览器选项卡未打开或浏览器本身未打开。它是否完全打开浏览器?在我看来,IE webbrowser 驱动程序比 chrome 或 firefox 驱动程序更复杂和错误。
  • 谢谢比尔。 IE 窗口会自动打开一个 get(URL)。使用 get 后,我​​使用我的 xPath("...") 来查找要单击的页面链接。我环顾四周,看到人们使用了很多 Firefox 和 chrome。 IE上不多。可能只是切换到Firefox。
  • 我会尝试在程序中放置一个调试点并逐步查找导致异常的行。随意展示您的第一个使用 XPath 的 findelement 命令的示例,您可能错过了某些内容(我们有时都会这样做)
  • @BillHileman 请允许我在IE webbrowser driver is a lot more complicated and buggy than chrome or firefox drivers 上不同意你的观点:) IMO,Web 应用程序的实现肯定会因浏览器而异。有时,IE 浏览器的行为与 chrome 或 firefox 浏览器相比有些不同,但并不复杂。毕竟Bug 是组成开发人员/测试人员团队的词:)
  • @BillHileman 感谢您告知有关 Chrome 驱动程序的信息。工作 100000 次更好更快。唯一的问题是程序结束时它不会关闭驱动程序。但这是吹毛求疵。

标签: java selenium xpath driver


【解决方案1】:

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

  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. 如果您的缩放设置未设置为 100%,则您忽略了缩放设置 dc.setCapability("ignoreZoomSetting",true);,您可能会遇到意外错误和异常。
  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);
    }
    

如果这能回答您的问题,请告诉我。

【讨论】:

  • 1.我把这个方法作为一种简单的方法来调用我在 main 中的每个类调用中的驱动程序,这些调用接受像 duck(driver()) 这样的 webdrivers。
  • @Sleepyfalcon 那么从其余的答案中你的问题得到解决了吗?
  • 2.这只是让 webdriver 与 IE 一起工作。我被困在它不喜欢我在这台计算机上使用 xPaths 的部分。 3. 如果您的意思是public static WebDriver driver() {},它会返回一个WebDriver,它在我在main 中调用的类中使用。 4.大概。这是我第一次在实习中真正使用 Selenium ¯_(ツ)_/¯。
  • 我喜欢你的帮助和所有帮助解决我的问题,但真正的问题在于当我调用 xPath 时它会崩溃。我认为它与屏幕大小有关。不确定这会如何影响 xPath。无论如何感谢您的帮助。
  • 我认为与其返回 WebDriver,您应该通过将代码放在 main() 或 @test 注释下来寻找最佳实践。让我知道这是否回答了您的问题。谢谢
猜你喜欢
  • 2014-01-21
  • 1970-01-01
  • 2011-03-30
  • 1970-01-01
  • 2020-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多