【问题标题】:nosuchwindowexception Selenium with PhantomJS Javanosuchwindowexception Selenium 与 PhantomJS Java
【发布时间】:2018-12-17 18:01:37
【问题描述】:

我正在使用无头 PhantomJS 浏览器使用带有 selenium 的 phantomjs 驱动程序来自动化应用程序。 (selenium java 3.5.2版和phantomjs.exe)

我有一个场景,我将填写表单并提交它,然后浏览器关闭,关闭浏览器后我重用驱动程序引用来获取 URL。当我使用带有 selenium 2.47.0 的 firefox 驱动程序时,它运行良好。

现在我切换到 selenium phontamjsdriver 和 phantombrowser。在这里,当我调用 driver.get(url); 浏览器关闭后,它会抛出 nosuchwindowexception 表示窗口已关闭或处于非活动状态。但是,相同的代码正在使用 firefox 驱动程序

示例:

driver.get(url);// first time works  
submitForm(driver);//browser window gets closed.  
driver.get(url); 

最后一行抛出异常为:

nosuchwindowexception(selenium java with 3.5.2 version and phantomjs.exe). 

但与 selenium 2.4.7 的 firefoxbrowser 配合使用效果很好。

【问题讨论】:

  • 你是如何关闭浏览器的,driver.quit()driver.close() 并且你还在使用无头 FF 吗?
  • 我没有关闭带有幻像驱动程序的浏览器。它是应用程序的行为,一旦提交表单,浏览器窗口就会自动关闭。它在代码中以编程方式关闭
  • AFAIU ,您在应用程序中打开一个页面,然后单击某处并打开一个带有表单的新窗口,然后您填写表单并自行关闭,然后您使用 driver.get('some url') 导航到某个 URL,对吗?
  • 我在应用程序中打开一页,然后单击链接。它在同一个窗口中打开表格,然后我填写表格,然后它被它自己关闭了。然后我使用 driver.get("url") 打开具有相同驱动程序实例的浏览器。然后它抛出 nosuch 窗口异常。但相同的代码适用于 Firefox 浏览器。看起来幻象浏览器和firefox的行为不一样
  • 你在使用显式等待吗?你也可以分享你的代码吗?我需要先查看代码,然后才能得出任何结论。

标签: java selenium selenium-webdriver webdriver phantomjs


【解决方案1】:

首先,当您从 Selenium v​​2.47.0 迁移到 Selenium v​​3.5.2 时,值得一提的是,随着 的可用性发生了很多变化硒 3.x。现在 Selenium-WebDriverW3C 推荐候选 并且符合 WebDriver W3C Editor's Draft


NoSuchWindowException

NoSuchWindowException 类扩展 NotFoundException 并在尝试时主要抛出:

WebDriver.switchTo().window(String windowName);

现在,关于您的 用例、相关 HTML 和您的 代码块 的更多详细信息会给我们提供更多想法错误的。也许submitForm(driver)的定义是解决你问题的关键。


最佳实践

您可以遵循以下一些最佳做法来避免NoSuchWindowException

  • 始终将父窗口句柄存储在一个变量中,以便您可以按一般需要遍历回父窗口
  • 在调用 driver.switchTo().window(windowHandle); 之前,始终诱导 WebDriverwaitExpectedConditions 方法 numberOfWindowsToBe(int)
  • 一旦你调用 driver.switchTo().window(windowHandle); 诱导 WebDriverWait 结合 ExpectedConditions 方法 titleContains(java.lang.String title) 等待 页面加载 完成以在新打开的窗口中继续您的测试步骤
  • 要切换回父窗口,请使用之前存储的窗口句柄
  • 这是一个示例代码块,用于演示 Window/Tab 处理:

    package demo;
    
    import java.util.Set;
    
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class WINDOW_HANDLE_ITERATE_Firefox 
    {
        public static void main(String[] args) throws Exception 
        {
            System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
            WebDriver driver =  new FirefoxDriver();
            driver.get("http://www.google.com");
            String parent_window = driver.getWindowHandle();
            System.out.println("Parent Window Handle is: "+driver.getWindowHandle());
            System.out.println("Page Title is: "+driver.getTitle());
            ((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');");
            new WebDriverWait(driver,10).until(ExpectedConditions.numberOfWindowsToBe(2));
            Set<String> allWindows_1 = driver.getWindowHandles();
            System.out.println("Total Windows: "+allWindows_1.size());
            for(String hand1:allWindows_1)
            if(!parent_window.equals(hand1))
            {
                driver.switchTo().window(hand1);
                new WebDriverWait(driver,10).until(ExpectedConditions.titleContains("Facebook"));
                String first_child_window = driver.getWindowHandle();
                System.out.println("First Child Window Handle is: "+first_child_window);
                System.out.println("First Child Window Page Title is: "+driver.getTitle());
                driver.close();
            }
            driver.switchTo().window(parent_window);
            System.out.println("Current Window Handle is : "+driver.getWindowHandle()+ " which is same as "+parent_window +", which is the parent window handle" );
            driver.quit();
        }
    }
    
  • 控制台输出:

    1531917836983   geckodriver INFO    geckodriver 0.20.1
    1531917836993   geckodriver INFO    Listening on 127.0.0.1:9993
    1531917837915   mozrunner::runner   INFO    Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "-marionette" "-profile" "C:\\Users\\ATECHM~1\\AppData\\Local\\Temp\\rust_mozprofile.W5WqVulBNm9x"
    1531917842220   Marionette  INFO    Listening on port 35364
    1531917843126   Marionette  WARN    TLS certificate errors will be ignored for this session
    Jul 18, 2018 6:14:03 PM org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: W3C
    Parent Window Handle is: 4294967297
    Page Title is: Google
    Total Windows: 2
    First Child Window Handle is: 4294967301
    First Child Window Page Title is: Facebook – log in or sign up
    Current Window Handle is : 4294967297 which is same as 4294967297, which is the parent window handle
    

【讨论】:

  • 在我的场景中。我没有导航到新窗口或在新窗口中打开链接。我只有一个窗口,一旦提交及其应用程序行为就会被应用程序关闭。我没有明确地关闭窗口。所以在窗口关闭后,我无法重用相同的驱动程序实例来打开窗口,它会抛出异常。但使用 Firefox 可以正常工作。如果我创建新的 phantomdriver 实例并调用 driver.get(url)。它正在重定向到创建新会话的登录页面。
  • @RamRamesh 你能用with firefox its working fine的代码块更新主要问题吗?
  • myPage.getSubmit().click(); this.clickMsgBoxOK(driver,"此表单已成功提交。");-------一旦执行此代码,窗口将在验证警报框上的数据后获取。现在我正在尝试使用相同的驱动程序实例获取 url。通常它应该能够重新创建浏览器窗口实例并获取我正在请求的 url.driver.get(MYREQUESTS_URL);当我调用最后一行代码时,它会抛出异常。基本上我只有一个窗口并关闭并尝试使用我拥有的相同驱动程序实例重新创建窗口实例。
  • 它与 Firefox 的代码相同。 driver.get(url);// 第一次工作 submitForm(driver);// 浏览器窗口被关闭。 driver.get(url); // 抛出异常
  • 准确地说.... public void testSubmit (WebDriver driver) throws Exception { MyPage myPage = (myPage) PageFactory.createPage(MyPage.class, driver); MyPage.switchToMe(); MyPage.getSubmit().click(); MyPage.getSubmit().click(); this.clickMsgBoxYes(driver,"您即将提交此表单,您确定要继续吗?"); this.clickMsgBoxOK(driver,"此表单已成功提交。");driver.get(MYREQUESTS_URL); }
【解决方案2】:

这可能是在https://github.com/ariya/phantomjs/issueshttps://github.com/ariya/phantomjs/issuesPhantomJS 团队提出的问题

不幸的是,Selenium 中的屏幕截图仅捕获 DOM 而不是地址栏。如果您能够保存页面源,则可以提取 URL。查看此页面的页面源时,我看到列出各种 3rd 方应用程序的标签,里面有一个列出 URL 的内容源。

<meta name="twitter:app:url:googleplay" content="http://stackoverflow.com/questions/51255939/nosuchwindowexception-selenium-with-phantomjs-java">

这可能不适用于每个网站,但可以在某个地方查看。如果您是网站所有者,也可以尝试在自己中添加此标签。

【讨论】:

    猜你喜欢
    • 2017-10-15
    • 2014-09-04
    • 1970-01-01
    • 2017-10-12
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多