3

I want to work with multiple windows and tabs using selenium and python.

I am getting below error during script execution:- ( code is mentioned at last)

{f625f26d-cfcf-442c-b9fc-5e96a199cd43}
C:\Python34\lib\site-packages\selenium-2.47.1- py3.4.egg\selenium\webdriver\remot
e\webdriver.py:525: DeprecationWarning: use driver.switch_to.window instead
 warnings.warn("use driver.switch_to.window instead", DeprecationWarning)
 {cad6e3cf-9062-408e-a6f1-11e98813dc6c}

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

class GoogleTabs(unittest.TestCase):

 def setUp(self):
     self.driver = webdriver.Firefox()

 def test_google_search_page(self):
     driver = self.driver
     driver.get("http://www.cdot.in")
     window_before = driver.window_handles[0]
     print (window_before)
     driver.find_element_by_xpath("//a[@href='http://www.cdot.in/home.htm']").click()
     window_after = driver.window_handles[1]
     driver.switch_to_window(window_after)
     print (window_after)
     driver.find_element_by_link_text("ATM").click()
     driver.switch_to_window(window_before)


 def tearDown(self):
     self.driver.close()

if __name__ == "__main__":
unittest.main()
1
  • Is it an error or a warning? Commented Dec 22, 2015 at 9:53

3 Answers 3

3

As the warning says

DeprecationWarning: use driver.switch_to.window instead

you'll need to change your

driver.switch_to_window

with

driver.switch_to.window

But think about it: it is a warning, not an error! Your code should work, it is just telling you that that method is deprecated.

Sign up to request clarification or add additional context in comments.

Comments

0

You're getting a warning message, for new versions of selenium the method has changed.

Instead of using: driver.switch_to_window(window)

Try with: driver.switch_to.window

Where window is your variable.

Comments

0

I propose a very simple way to switch between windows without any hassles of playing with the handles yourself. https://gist.github.com/ab9-er/08c3ce7d2d5cdfa94bc7

def change_window(browser):
    """
    Simple window switcher without the need of playing with ids.
    @param browser: Current browser instance
    """
    curr = browser.current_window_handle
    all_handles = browser.window_handles
    for handle in list(set([curr]) - set(all_handles)):
        return browser.switch_to_window(handle)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.