On a recent project, I ran into an issue with Python Selenium webdriver. There's no easy way to open a new tab, grab whatever you need and return to original window opener.
Here's a couple people who ran into the same complication:
- http://stackoverflow.com/questions/17547473/how-to-open-a-new-tab-using-selenium-webdriver
- http://stackoverflow.com/questions/6421988/webdriver-open-new-tab/9122450#9122450
- https://groups.google.com/forum/#!topic/selenium-users/kah4iEPRopc
- ... and many many more.
So, after many minutes (read about an hour) of searching, I decided to do find a quick solution to this problem.
First thing, I've broken down all the steps that were required to do by my program:
- Open a new window/tab by simulating a click on a link
- Add focus on this new tab
- Wait for an element on the new page to be rendered (ui.WebDriverWait)
- Do whatever I have to do on this new page
- Close the tab
- Return focus on original window opener
-
First off, we import our packages:
import selenium.webdriver as webdriver import selenium.webdriver.support.ui as ui from selenium.webdriver.common.keys import Keys from time import sleep
-
Let's do it:
browser = webdriver.Firefox() browser.get('https://www.google.com?q=python#q=python') first_result = ui.WebDriverWait(browser, 15).until(lambda browser: browser.find_element_by_class_name('rc')) first_link = first_result.find_element_by_tag_name('a') # Save the window opener (current window, do not mistaken with tab... not the same) main_window = browser.current_window_handle # Open the link in a new tab by sending key strokes on the element # Use: Keys.CONTROL + Keys.SHIFT + Keys.RETURN to open tab on top of the stack first_link.send_keys(Keys.CONTROL + Keys.RETURN) # Switch tab to the new tab, which we will assume is the next one on the right browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB) # Put focus on current window which will, in fact, put focus on the current visible tab browser.switch_to_window(main_window) # do whatever you have to do on this page, we will just got to sleep for now sleep(2) # Close current tab browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 'w') # Put focus on current window which will be the window opener browser.switch_to_window(main_window)
Simple!
Hi This is regarding selenium automation I am trying to switch between tabs using window handler could you tell me what am I doing wrong.
go to Gmail
open bing.com in next tab
I am using browser.execute_script('''window.open("http://bings.com","_blank");''')
to open new tab as Keys.COMMAND +’t’ is not working
Also, for switching between windows I am trying to use window handler but could not get it work.Could you please let me know what am I doing wrong and what needs to be done thanks.
browser = self.resource_handler.driver
browser.set_page_load_timeout(60)
browser.implicitly_wait(15)
browser.get("http://google.com")
main_window = browser.current_window_handle
main_window = browser.getWindowHandle()
print main_window
time.sleep(5)
browser.execute_script('''window.open("http://bings.com","_blank");''')
time.sleep(2)
second_window = browser.current_window_handle
print browser.title
------------------------
body = browser.find_element_by_tag_name("body").send_keys(Keys.COMMAND+Keys.TAB)
browser.execute_script('''window.open("http://bings.com","_blank");''')
browser.find_element_by_tag_name("body").send_keys(Keys.COMMAND + Keys.ENTER)
time.sleep(5)
print 'new oldtab opened'
print browser.title
browser.switch_to_window(main_window)
print browser.switch_to_window(browser.window_handles[1])
print 'newtab opened'
time.sleep(5)