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
7,426 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)


Hope this Helps!!

To learn more, join the online course to do Masters in Python.

Thanks!

answered Jul 29, 2019 by Abha
• 28,140 points

edited Apr 18, 2022 by Sarfaraz

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
15,641 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,150 points
15,139 views
0 votes
2 answers

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,500 points
12,617 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
8,572 views
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,910 points
6,629 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
• 28,140 points
9,435 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
• 28,140 points
22,003 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP