8

I am exporting my selenium test to python and running the test using the shell. I get to a certain point where I click on a link and the web-driver then opens a completely new Firefox window and then I get an error saying that the driver can't find the input option on the page. I think the problem is that when it opens the new window the driver is not running on the new window, which is why it can't find the input option. How do I get the script to stay on the same window or switch it's focus to the new window?

Thanks!

seeiespi
  • 3,628
  • 2
  • 35
  • 37

1 Answers1

4

I don't know how to do it in python, but there should be a function to switch to a new window.

In Java, I do this:

Set<String> availableWindows = driver.getWindowHandles();
for (String windowHandle : availableWindows) {
    driver.switchTo().window(windowHandle);
    if (getTitle().contains(TITLE_TO_MATCH)){
           driver.manage().window().maximize();
            return;
    }
}

(The function want is driver.switchTo().window(NameOrHandle))

Nathan Merrill
  • 7,648
  • 5
  • 37
  • 56
  • 2
    Thanks, it's very similar in python.` `aw = driver.window_handles` `driver.switch_to_window(aw[1])` If you only have one window open then the original window will be aw[0] and the new window will be aw[1]. If you have more windows open then you can always print them out and input them manually or just keep track of their position in the list. Thanks again MrTi – seeiespi Aug 09 '13 at 17:15
  • @JohnDemetriou Tabs really aren't the way to do things in selenium. You can't run them concurrently, and you have to simulate a keystroke to switch to the new tab. – Nathan Merrill Mar 23 '15 at 21:11
  • yeah but clicking a link opens a new tab in my test – John Demetriou Mar 24 '15 at 05:21
  • 1
    @JohnDemetriou Oh. To switch to the new tab, I would recommend closing the current tab by simulating the keystrokes Ctrl-W. If you need both tabs open, then simulate Ctrl-Tab. – Nathan Merrill Mar 24 '15 at 12:50