Home > Enterprise >  Python Selenium 'WebDriver' object has no attribute 'switch_to_window'
Python Selenium 'WebDriver' object has no attribute 'switch_to_window'

Time:10-25

My Code do what it should do until it reach the point when a button is clicked and a second PDF window opens. I try to switch to the PDF window to close it but it does not work, my Code after the button is clicked looks like:

while len(browser.window_handles) < 2:
    sleep(1)

browser.switch_to_window(browser.window_handles[1])
sleep(0.5)
browser.close()
sleep(0.5)
browser.switch_to_window(browser.window_handles[0])

With this Code i get the error message: AttributeError: 'WebDriver' object has no attribute 'switch_to_window'

I already used this code in another project with a older selenium version and there it works fine.. In my current project I use selenium 4.5.0. I am happy for any hint you guys can make.

CodePudding user response:

Selenium 4 no more supports switch_to_window method.
switch_to.window should be used instead, as following:

browser.switch_to.window(browser.window_handles[0])
  • Related