Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (19.7k points)

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.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']"))


    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']/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='')).

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):




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

1 Answer

0 votes
by (62.9k points)

Below are the steps to work with tabs in the same browser:

  1. Open a new tab using Ctrl + t

  2. By using this the driver control automatically switches to the newly opened tab

  3. Perform the required operations here.

  4. In the next step, you'd have to switch back to the old tab using Ctrl + Tab. You have to keep pressing these keys unless you reach the desired tab.

  5. Once the desired tab is reached, then perform the operations you want to perform in that tab.

Then, get the present window handle and switch to a new tab using Ctrl + t


String windowHandle = driver.getWindowHandle();

driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");

Check the size of the output of getWindowHandles().

Then Use: 

ArrayList tabs = new ArrayList (driver.getWindowHandles());



The control is now in the new tab-

 driver.get("Your application URL");

//perform other operations on new tab.

Now, you can switch to the previous tab using Ctrl + Tab:


 driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");



You can perform the steps using Action class too.

For navigating left to the right side:

Actions action= new Actions(driver);


For navigating right to the left side:

Actions action= new Actions(driver);


To get more details to check here

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s Selenium certification!

Browse Categories