3

There is a link embedded in a web element in the Main Tab, I want to open that link in a new tab in the same window using Selenium Webdriver and python. Perform some tasks in the new tab and then close that tab and return back to the main tab. Manually we will do this by right-clicking on the link and then select "open in new tab" to open that link in new tab.

I am new to Selenium. I am using Selenium and BeautifulSoup to web scrape. I only know how to click on a link. I have read through many posts but couldn't find a proper answer

url = "https://www.website.com"
path = r'path_to_chrome_driver'
drive = webdriver.Chrome(path)
drive.implicitly_wait(30)
drive.get(url)
source = drie.page_source

py_button = driver.find_element_by_css_selector("div[data-res-position = '1']")
py_button.click()

I expect the link in div[data-res-position = '1'] to open in a new tab

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Devesh
  • 127
  • 1
  • 10

3 Answers3

1

Following import:

selenium.webdriver.common.keys import Keys

You have to send key:

py_button.send_keys(Keys.CONTROL + 't')
frianH
  • 7,295
  • 6
  • 20
  • 45
Amin KAMAL
  • 13
  • 4
  • Thank you for your answer, but now I am getting an error "ElementNotInteractableException: Message: element not interactable (Session info: chrome=76.0.3809.132)" – Devesh Aug 30 '19 at 18:50
  • Maybe a time.sleep (10) between find_element and send_key – Amin KAMAL Aug 30 '19 at 19:19
1

This is trick can be achieve if your locator return a correct href.

Try this first:

href = driver.find_element_by_css_selector("div[data-res-position = '1']").get_attribute("href")
print(href)

If you get correct href, you can do:

#handle current tab
first_tab = driver.window_handles[0]

#open new tab with specific url
driver.execute_script("window.open('" +href +"');")

#hadle new tab
second_tab = driver.window_handles[1]

#switch to second tab
driver.switch_to.window(second_tab)

#switch to first tab
driver.switch_to.window(first_tab)

Hope this helps.

frianH
  • 7,295
  • 6
  • 20
  • 45
1

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank you very much, it worked. But How do I close the new tab after performing some tasks on it and go back to main tab. – Devesh Sep 04 '19 at 10:51
  • @Devesh Checkout the answer update and let me know the status. – undetected Selenium Sep 04 '19 at 11:17
  • Well Sir operations I want to perform are:
    1) Open main tab
    2) Click on Link embedded in this tab and open it in new tab
    3) Switch focus/handle to new tab
    5) Perform tasks using BeautifulSoup
    6) Close new tab
    7) Switch focus/handle to main tab
    I am done with steps 1,2,5. Could you please help in doing others steps. I checked the update it didn't work.
    – Devesh Sep 07 '19 at 17:57
  • *Update* I found the solution. switch_to.window() worked for me. Thanks – Devesh Sep 07 '19 at 19:12