Selenium with Webdriver - Switch to child window without name

♀尐吖头ヾ 提交于 2019-12-11 14:13:27

问题


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:

"I execute in selenium "lambda function""

The webdriver acceptance fails...


回答1:


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


来源:https://stackoverflow.com/questions/28154957/selenium-with-webdriver-switch-to-child-window-without-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!