Open Web in New Tab Selenium + Python

Open web in new tab Selenium + Python

Editor's note: This answer no longer works for new Selenium versions. Refer to this comment.


You can achieve the opening/closing of a tab by the combination of keys COMMAND + T or COMMAND + W (OSX). On other OSs you can use CONTROL + T / CONTROL + W.

In selenium you can emulate such behavior.
You will need to create one webdriver and as many tabs as the tests you need.

Here it is the code.

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

driver = webdriver.Firefox()
driver.get("http://www.google.com/")

#open tab
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')
# You can use (Keys.CONTROL + 't') on other OSs

# Load a page
driver.get('http://stackoverflow.com/')
# Make the tests...

# close the tab
# (Keys.CONTROL + 'w') on other OSs.
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 'w')


driver.close()

How to open a particular link in a new tab in selenium/python

Here is the solution.

Sourcecontrol = driver.find_element_by_xpath('//li[@class="menu-item"]/a[contains(.,"Source Control")]')
Sourcecontrol.click();
Changerequest=driver.find_element_by_xpath( '//td[@class="confluenceTd"]/a[contains(.,"Change: ")]')
testvalue = Changerequest.get_attribute('href')
driver.execute_script("window.open(arguments[0])",testvalue)

How to open a link embeded in a webelement with in the main tab, in a new tab of the same window using Control + Click of Selenium Webdriver

As there is a link embedded within in the webelement in the Parent Tab, to open the link in a New Tab in the same window using Selenium and Python you can use the following solution:

To demonstrate the workflow the url https://www.google.com/ was opened in the Parent Tab and then open in new tab functionalty is implemented through ActionChains methods key_down(), click() and key_up() methods.

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.common.keys import Keys
    import time

    options = webdriver.ChromeOptions()
    options.add_argument("start-maximized")
    driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    driver.get("https://www.google.com/")
    link = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Gmail")))
    ActionChains(driver).key_down(Keys.CONTROL).click(link).key_up(Keys.CONTROL).perform()
  • Note: You need to replace (By.LINK_TEXT, "Gmail") with your desired locator e.g. ("div[data-res-position = '1']")

  • Browser Snapshot:

tab_actions

You can find a relevant Java based solution in Opening a new tab using Ctrl + click combination in Selenium Webdriver


Update

To shift Selenium's focus to the newly opened tab you can find a detailed discussion in Open web in new tab Selenium + Python

How to open in a new tab with selenium and python?

In my experience it will be difficult to achieve a perfect "one fits all" solution involving the (context menu - new tab) combination, and I tend to keep clear of all the headache it can bring.

My strategy would be a bit different, and, on a case by case basis, I'd use something like:

base_window = driver.current_window_handle  # this goes after you called driver.get(<url here>)

my_element=driver.find_element_by_xpath(...) #or whatever identification method
my_element.send_keys(Keys.CONTROL + Keys.ENTER)

driver.switch_to.window(driver.window_handles[1]) #switch to newly opened tab

driver.switch_to.window(base_window) # switch back to the initial tab

An alternative workaround is using hrefs - first open a new tab, then load the fetched href(s). Here's an example:

url='https://www.instagram.com/explore/tags/cars/?hl=en'
driver.get(url)
base_window = driver.current_window_handle

a_tags=driver.find_elements_by_xpath("//div[@class='v1Nh3 kIKUG _bz0w']//a")
hrefs=[a_tag.get_attribute('href') for a_tag in a_tags] #list of hrefs

driver.execute_script("window.open();") #open new tab
driver.switch_to.window(driver.window_handles[1]) #switch to new tab
driver.get(hrefs[0]) #get first href for example
driver.close() #close new tab
driver.switch_to.window(base_window) #back to initial tab


Related Topics



Leave a reply



Submit