问题
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