使用Webdriver的Selenium-切换到没有名称的子窗口 [英] Selenium with Webdriver - Switch to child window without name

查看:84
本文介绍了使用Webdriver的Selenium-切换到没有名称的子窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用网络驱动程序和代码接收进行验收测试。我对此有些陌生,所以请多多包涵。我试图切换到单击按钮后生成的子窗口:

I'm performing acceptance testing with webdriver and codeception. I'm a bit new to it, so please bear with me. I am trying to switch to a child window that is generated after clicking a button:

<input class="submit_btn" type="button" onclick="openHAWin(this.form.purchase_clinic.value)" value="add" name="add_ha">

由于该页面的名称没有嵌入代码中,也没有目标页面本身,尝试使用以下推荐的代码切换到子页面:

As there is no name for this page embedded in the code, nor on the target page itself, I attempted to use the following recommended code to switch to the child page:

$I->executeInSelenium(function (\Webdriver\Session $webdriver) {
$handles=$webdriver->window_handles();
$last_window = end($handles);
$webdriver->focusWindow($last_window);});

但是,以上代码在使用它的步骤中引发了错误:

However, the above code throws an error in the step that uses it:


我在硒中执行 lambda函数

"I execute in selenium "lambda function""

webdriver接受失败...

The webdriver acceptance fails...

推荐答案

当我确定只有2个窗口/选项卡时,我有正在使用的方法可能一次打开(父级和新级),但是它在Java中,因此您必须将其移植到环境中。这段代码是基于我对这个门户网站和我的补充的研究。
基本上执行以下操作:获取所有可用的窗口并切换到非父窗口。

I have method that I'm using when I'm sure that only 2 windows/tabs may be opened at one time (parent and new one), but it is in java so you have to port it to your env. This code is based on my research on this portal + mine additions. Basically what is done below: get all available windows and switch to one that is not a parent.

    String parent = driver.getWindowHandle();

    Thread.sleep(1000);
    Set<String> availableWindows = driver.getWindowHandles();
    String newWindow = null;
    for (String window : availableWindows) {
        if (!parent.equals(window)) {
            newWindow = window;
        }
    }
    if (newWindow != null) {
        WebDriver op = driver.switchTo().window(newWindow);
        //("Driver switched to new window: " + op.getTitle() + " | " + op.getCurrentUrl());
    ]

这篇关于使用Webdriver的Selenium-切换到没有名称的子窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆