4

I use Firefox. I click a button but the page opens as a new tab and selenium stays focused on the original page with the button. I'm trying to switch and get the new url.

time.sleep(4)
second_driver.switch_to.window(second_driver.window_handles[1])
print(second_driver.current_url)

I get:

selenium.common.exceptions.InvalidArgumentException: Message: Expected "handle" to be a string, got [object Undefined] undefined

I also tried the following but it didn't have an effect. I still got the url of the old page.

second_driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)
Jackkt
  • 55
  • 5
  • could you please try this in the second line. second_driver.switch_to.window(str(second_driver.window_handles[1])) – UnknownBeast Oct 01 '20 at 18:08
  • 1
    https://stackoverflow.com/questions/63949719/ive-got-invalidargumentexception-when-i-used-switch-to-window-by-selenium-pyt It's a firefox bug. I'd suggest switching your excutable path to a webdriver manager. – Arundeep Chohan Oct 01 '20 at 18:09
  • second_driver.switch_to.window(str(second_driver.window_handles[1])) gives the same error – Jackkt Oct 01 '20 at 19:26
  • I would try getting the handle and then passing it... so handle = second_driver.window_handles[1] and then second_driver.switch_to.window(handle) Also note that the order of the handles in this array is not guaranteed across drivers. – pcalkins Oct 01 '20 at 19:27

1 Answers1

4

This was a Geckodriver bug. It was resolved on the 0.27.0 version release: https://github.com/mozilla/geckodriver/releases

I have used to experience the same issue with the 0.24.0 version and then I have upgraded the Geckodriver to this version, and then the issue stopped happening.

However, I recommend using always the latest version of Geckodriver. I have checked with the 0.29.0 version and the bug is also gone.

Javier Sorella
  • 151
  • 1
  • 5