• support@macstocktech.com
  • +91 9709487145
  • Message us today! +91 9709487145

Logo
  • Home
  • Services
  • Expertise
  • Team
  • Blogs
  • Contact

How to open a link in new Tab in Selenium Python | Open Any Link in New Tab and Switch Between Tabs Using Selenium

  • Home
  • Blog Details
  • May 8 2021
  • Macstock Tech

Hi, I think you are struggling to open a link in new tab in selenium Python. Let me fix your problem. In this post, I am going to explain you How to Open any Link which is available on any site in new tab and I am also gonna explain how to Open a normal link in new tab. I will also explain How to Switch Between Tabs. That’s why, read the post till end and don’t forget to share it with your friends because I know that your friends are also struggling in this thing. So, let’s get started.

How to open any Link which is available on any site?

It’s not such a big problem. Let me explain you how to do that. I am going to use the Google Search Page for demonstration, you should do the steps with the site you want to.

First of all Let’s open the Page which contains the link/links.

I had searched “youtube” on the Google Search Engine and I am using the Search Results Page For Demonstration

Step 1:- Right Click on the Link You want to Open In New Tab and Click On Inspect Button.

Now, You will See a Developer Console Appeared on the bottom or right of the page.

Step 2:- Now Right Click on the ‘a’ tag which contains the link and go to Copy and click on XPath.

Python
from selenium import webdriver

Step 4:- Now we will Initialize Chrome Driver and Get the Site Url.

Python
driver = webdriver.Chrome()
driver.get('https://www.google.com/search?q=youtube')

Step 5:- Now we will Find The ‘a’ tag by it’s XPath which we copied earlier and then get the href attribute of the a tag.

Python
url = driver.find_element_by_xpath('//*[@id="rso"]/div[1]/div/div/div/div/div/div[1]/a').get_attribute('href')

Step 6:- Now we will execute a Javascript Script which helps to open a new tab and then we will Use the webdriver function to switch to the new opened tab.

Python
driver.execute_script('window.open('');')
driver.switch_to.window(driver.window_handles[1])

Step 7:- Then we will get the link we fetched by finding href attribute of the element.

Python
driver.get(url)

And Here you go, You Opened the Link in new Tab. If you want to close the tab then Just Use This Code.

Python
driver.close()

Or if you want to Switch Back to Previous Tab Then Use This code.

Python
driver.switch_to.window(driver.window_handles[0])

How to Open a Normal Link in New Tab?

Like Shown Above, This is also very easy. Just we need is to get a link and then open a new tab by executing a JS Script and then switch to the New Tab and Then Get the url. Here is the Code For that.

Python
from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://www.google.com/')

driver.execute_script('window.open('');')
driver.switch_to.window(driver.window_handles[1])
driver.get('https://www.youtube.com/')

# For Closing The Second Tab
driver.close()

# For Switching Back to Main Tab
driver.switch_to.window(driver.window_handles[0])

And Here you go, I had explained How to Open, Switch and Close Tabs using Python Selenium. I hope you liked it and It helped you. Thanks For Reading and Please Don’t Forget to Share 🙂

Previous Post
How to Fix Lag in Unity Engine | 100% Working 5 Methods For 2 GB or 4 GB RAM PC
Next Post
How to remove chrome is being controlled by automated test software in Selenium Python

Leave a Comment Cancel reply

Recent Posts

  • How to Use Laptop Display and External Monitor as Dual Monitor
  • Create a Dynamic Website with Flask without using any Database
  • Choosing the Best Laptop for Coding
  • How to make a Captcha Solver in Python without using Machine Learning
  • How to remove chrome is being controlled by automated test software in Selenium Python

Categories

  • Guide 3
  • Problem Solving 3
  • Projects 2
  • Python 4
  • Tech 4
  • Tutorials 4

Recent Comments

  • avenue17 on Create a Dynamic Website with Flask without using any Database

Tags

connectlaptop desktop flask flaskapp flaskdevelopment laptop monitor monitorsetup python pythonproject webdev
Shape
Logo

Macstock Tech offers IT and digital solutions through web development, e-commerce, marketing, and social media services to help businesses grow online.

Quick Links

  • Home
  • Services
  • Expertise
  • Team
  • Contact Us
  • Our Blogs

Contact Info

  • support@macstocktech.com
  • +91 9798149880
    +91 9709487145

© Copyright 2025 Macstocktech.com | All Rights Reserved

  • Contact