Not able to open a new URL in a new tab in Selenium

0 votes

I want to open different tabs with different URLs using Selenium WebDriver & Python.

I used the below code but it is not working for me:

driver = webdriver.Chrome()
driver.get(url_1)
time.sleep(10)
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL+'t')
url_2 = 'https://www.gmail.com'
driver.get(item_2)
May 21, 2018 in Selenium by Perry
• 17,100 points
18,683 views

1 answer to this question.

0 votes

There is a bug in ChromeDriver that does not allow "Ctrl + T" from working.

 But you can open a link in a new tab and then switch to new tab using switch_to.window()

Below is a sample code:

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get("https://www.gmail.com")

# To open a link in a new tab
actions = Action(driver)
find = driver.find_element_by_link_text('ABC')
actions.key_down(Keys.CONTROL).click(find).key_up(Keys.CONTROL).perform()

driver.switch_to.window(driver.window_handles[-1])
driver.get("https://stackoverflow.com")

With this, the driver.get() would be performed in a newly opened tab.

answered May 21, 2018 by Meci Matt
• 9,460 points

Thank you for the answer.
It worked for me.
I just had to replace 

actions = Action(driver) 
with
actions = ActionChains(driver)

 then it works great!

find = driver.find_element_by_link_text('ABC')
what should replace with ABC 
Hey @insane, what exactly are you trying to do?

Related Questions In Selenium

0 votes
1 answer

I tried to open a new website when a new tab is opened using Selenium Webdriver

Try this code: ArrayList<String> tabs = new ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,500 points
4,455 views
0 votes
2 answers

How to open a link in a new tab?

try this :- driver.findElement(By.linkText("Business")).sendKeys(K ...READ MORE

answered Jan 11, 2019 in Selenium by kaloo
1,525 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,332 views
0 votes
2 answers

How to open a new tab in existing browser window using Javascript Executor?

Hi Meenal, you can use executeScript() method ...READ MORE

answered Jul 22, 2019 in Selenium by Abha
• 28,140 points

edited Oct 7, 2021 by Sarfaraz 11,090 views
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,877 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
13,758 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
9,182 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
7,255 views
0 votes
8 answers

How to open a link in new tab of chrome browser using Selenium WebDriver?

This below code works for me in ...READ MORE

answered Dec 14, 2020 in Selenium by Gitika
• 65,770 points
103,114 views
0 votes
1 answer

Open new tab instead of a new window in Selenium WebDriver

The current version of Selenium do not ...READ MORE

answered Jun 22, 2018 in Selenium by Meci Matt
• 9,460 points
3,957 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