Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I switch to the active tab in Selenium?

We developed a Chrome extension, and I want to test our extension with Selenium. I created a test, but the problem is that our extension opens a new tab when it's installed, and I think I get an exception from the other tab. Is it possible to switch to the active tab I'm testing? Or another option is to start with the extension disabled, then login to our website and only then enable the extension. Is it possible? Here is my code:

def login_to_webapp(self):     self.driver.get(url='http://example.com/logout')     self.driver.maximize_window()     self.assertEqual(first="Web Editor", second=self.driver.title)     action = webdriver.ActionChains(driver=self.driver)     action.move_to_element(to_element=self.driver.find_element_by_xpath(xpath="//div[@id='header_floater']/div[@class='header_menu']/button[@class='btn_header signature_menu'][text()='My signature']"))     action.perform()     self.driver.find_element_by_xpath(xpath="//ul[@id='signature_menu_downlist'][@class='menu_downlist']/li[text()='Log In']").click()     self.driver.find_element_by_xpath(xpath="//form[@id='atho-form']/div[@class='input']/input[@name='useremail']").send_keys("[email]")     self.driver.find_element_by_xpath(xpath="//form[@id='atho-form']/div[@class='input']/input[@name='password']").send_keys("[password]")     self.driver.find_element_by_xpath(xpath="//form[@id='atho-form']/button[@type='submit'][@class='atho-button signin_button'][text()='Sign in']").click() 

The test fails with ElementNotVisibleException: Message: element not visible, because in the new tab (opened by the extension) "Log In" is not visible (I think the new tab is opened only after the command self.driver.get(url='http://example.com/logout')).

Update: I found out that the exception is not related to the extra tab, it's from our website. But I closed the extra tab with this code, according to @aberna's answer:

def close_last_tab(self):     if (len(self.driver.window_handles) == 2):         self.driver.switch_to.window(window_name=self.driver.window_handles[-1])         self.driver.close()         self.driver.switch_to.window(window_name=self.driver.window_handles[0]) 

After closing the extra tab, I can see my tab in the video.

like image 667
Uri Avatar asked Feb 25 '15 09:02

Uri


People also ask

How do I switch between multiple windows in Selenium?

In Selenium, when we have multiple windows in any web application, the approach may need to switch control among several windows i.e from one window to another to perform any action and we can achieve this by using switchto(); method.

How do I switch between windows in Python Selenium?

window_handles is used for working with different windows. It stores the window ids that are used for switching. switch_to. window method is used for switching between the windows with the help of window_handles ids.

What is active element in Selenium?

activeElement() Switches to the element that currently has focus within the document currently "switched to", or the body element if this cannot be detected.


2 Answers

This actually worked for me in 3.x:

driver.switch_to.window(driver.window_handles[1]) 

window handles are appended, so this selects the second tab in the list

to continue with first tab:

driver.switch_to.window(driver.window_handles[0]) 
like image 112
Avaricious_vulture Avatar answered Oct 03 '22 00:10

Avaricious_vulture


Some possible approaches:

1 - Switch between the tabs using the send_keys (CONTROL + TAB)

self.driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB) 

2 - Switch between the tabs using the using ActionsChains (CONTROL+TAB)

actions = ActionChains(self.driver)       actions.key_down(Keys.CONTROL).key_down(Keys.TAB).key_up(Keys.TAB).key_up(Keys.CONTROL).perform() 

3 - Another approach could make usage of the Selenium methods to check current window and move to another one:

You can use

driver.window_handles 

to find a list of window handles and after try to switch using the following methods.

- driver.switch_to.active_element       - driver.switch_to.default_content - driver.switch_to.window 

For example, to switch to the last opened tab, you can do:

driver.switch_to.window(driver.window_handles[-1]) 
like image 33
aberna Avatar answered Oct 03 '22 00:10

aberna