How to switch different browser tabs using Python Selenium?

0 votes
How to switch different browser tabs using Python Selenium?
Jul 29, 2019 in Selenium by Rohan
3,632 views

1 answer to this question.

0 votes

Hello Rohan, you can use the following lines of code to switch different browser tabs using Python Selenium Webdriver:

import selenium.webdriver as webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.keys import Keys
from time import sleep

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

main_window = browser.current_window_handle
first_link.send_keys(Keys.CONTROL + Keys.RETURN)

browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)
browser.switch_to_window(main_window)

sleep(2)

browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 'w')
browser.switch_to_window(main_window)
answered Jul 29, 2019 by Abha
• 27,930 points

Related Questions In Selenium

0 votes
1 answer

How to switch between two windows in browser using Selenium java

Yes, this is possible. First, you need ...READ MORE

answered Sep 25, 2018 in Selenium by Meci Matt
• 9,460 points
8,177 views
0 votes
2 answers
0 votes
1 answer

Installing Selenium Webdriver with Python package

Hey Hemant, for installing Selenium Webdriver with ...READ MORE

answered May 8, 2019 in Selenium by Anvi
• 14,090 points
2,419 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,510 points
3,123 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,890 points
2,356 views
0 votes
1 answer

How to switch between tabs on a browser and get titles of each tab using Selenium?

Hi Abhimanyu, you can use following code ...READ MORE

answered Jul 22, 2019 in Selenium by Abha
• 27,930 points
2,881 views
0 votes
1 answer

How to open new tab in same browser and switch between them using Selenium?

Hi Mugdha, you can use following code ...READ MORE

answered May 24, 2019 in Selenium by Abha
• 27,930 points
7,444 views