3

I've been searching around for answers, but I cannot get my code to work. What I would like to happen is that I can open a tab, open a new webpage, and then close that same tab. From there, once i'm done doing my work, I'd like it to quit the program. As of now, it will not open a new tab, close the new tab, and quit the application. What is doing right now, is that it opens up google, then loads stackoverflow in the same tab.

Right now, I have...

driver = webdriver.Firefox()
driver.get("https://www.google.com")
time.sleep(5)
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')
time.sleep(5)
driver.get("https://www.stackoverflow.com")
time.sleep(5)
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 'w')
time.sleep(5)
driver.Dispose()

Does anyone have any ideas?

Thanks

Matt I
  • 165
  • 3
  • 11
  • Possible duplicate of [Switching into new window using selenium after button click](https://stackoverflow.com/questions/47082900/switching-into-new-window-using-selenium-after-button-click) – undetected Selenium Dec 17 '17 at 09:06
  • Using iterator - https://stackoverflow.com/questions/46251494/best-way-to-keep-track-of-windows-with-selenium-in-ie11/46346324#46346324 – undetected Selenium Dec 17 '17 at 09:12

1 Answers1

3

Try below code to get desired result:

driver = webdriver.Firefox()
driver.get("https://www.google.com")
# Get current window/tab ID
current_window = driver.current_window_handle
# Open StackOverflow in new window/tab
driver.execute_script("window.open('https://www.stackoverflow.com')")
# Get new window/tab ID
new_window = [window for window in driver.window_handles if window != current_window][0]
# Switch to new window/tab
driver.switch_to.window(new_window)
# Close new window/tab
driver.close()
# Switch to initial window/tab
driver.switch_to.window(current_window)
# Quit Webdriver
driver.quit()
Andersson
  • 51,635
  • 17
  • 77
  • 129
  • Thanks for this! it worked. I just have a question about this implementation. 1. Is there a benefit or reason why you used driver.execute_script vs driver.get? 2. for the line: new_window = [window for window in driver.window_handles if window != current_window][0] Why does this work? Is this just acting as a place holder? – Matt I Dec 19 '17 at 03:42
  • 1. `driver.execute_script("window.open(URL)` allows to open URL in new tab while `driver.get(URL)` - in the same tab. 2. I'm not sure I understand what do you mean by *placeholder* in this case... This line allows you to get window from the list of currently available windows which is different from initial window... – Andersson Dec 19 '17 at 05:56
  • Much appreciated for the clarification. For #2, I just meant, placeholder, as the window it self. So currentwindow would be a place holder for tab 0(google), then new_window would be a placeholder for tab 1(stackoverflow) – Matt I Dec 19 '17 at 08:37
  • It's an ID for window. Looks like `'CDwindow-24a4cac9-a500-4cfa-b0b1-fe5490612705'` – Andersson Dec 19 '17 at 08:43
  • You can [accept the answer](https://stackoverflow.com/help/accepted-answer) if it solved your problem – Andersson Dec 19 '17 at 08:47