How to switch different browser tabs using Python Selenium?

0 votes
How to switch different browser tabs using Python Selenium?
Jul 29 in Selenium by Rohan
182 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 by Abha
• 27,200 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,440 points
4,166 views
0 votes
1 answer

Installing Selenium Webdriver with Python package

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

answered May 8 in Selenium by Anvi
• 13,960 points
280 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,430 points
1,190 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,510 points
1,274 views
0 votes
1 answer
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 in Selenium by Abha
• 27,200 points
1,518 views