0

Here is an example script:

from selenium import webdriver

driver = webdriver.Firefox()

driver.get('https://www.startpage.com/en/')

driver.find_element_by_xpath('//*[@id="query"]').send_keys('Example')

driver.find_element_by_xpath('/html/body/div[1]/main/div[1]/section/div[1]/div[1]/form/button[2]/span[2]').click()

driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div[1]/div[2]/div[1]/div/section[5]/div[1]/a/h3').click()

At the end, it clicks something that opens a new tab. I want to close the new tab and continue working with the original tab

Guy
  • 46,488
  • 10
  • 44
  • 88
TheRealTengri
  • 1,201
  • 1
  • 7
  • 17

2 Answers2

0

driver.window_handles is the object for browser tabs.

Assuming that you want to close second tab (new tab), this works.

# continue from your code
driver.switch_to.window(driver.window_handles[1])
driver.close()

Just change i of window_handles[i] for other tab.

shimo
  • 2,156
  • 4
  • 17
  • 21
0

To close the new tab and continue working with the original tab you have to:

  • Switch to the new tab inducing WebDriverWait for number_of_windows_to_be(2) and close().
  • Switch back to the parent tab.

    • 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
      
      options = webdriver.ChromeOptions() 
      options.add_argument("start-maximized")
      options.add_experimental_option("excludeSwitches", ["enable-automation"])
      options.add_experimental_option('useAutomationExtension', False)
      driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
      
      driver.get('https://www.startpage.com/en/')
      print("Initial Page Title is : %s" %driver.title)
      windows_before  = driver.current_window_handle
      print("First Window Handle is : %s" %windows_before)
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.search-form__input"))).send_keys("Example")
      driver.find_element_by_css_selector("span.search-form__button-icon").click()
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//h3[@class='w-gl__label']//following::h3[1]"))).click()
      WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2))
      windows_after = driver.window_handles
      new_window = [x for x in windows_after if x != windows_before][0]
      driver.switch_to.window(new_window)
      print("Page Title after Tab Switching is : %s" %driver.title)
      print("Second Window Handle is : %s" %new_window)
      driver.close()
      driver.switch_to.window(windows_before)
      print("Page Title after second Tab Switching is : %s" %driver.title)
      print("Current Window Handle is : %s" %windows_before)
      
    • Console Output:

      Initial Page Title is : Startpage.com - The world's most private search engine
      First Window Handle is : CDwindow-18CCC5501A5F68CBE1C3094D0D0B419D
      Page Title after Tab Switching is : YouTube
      Second Window Handle is : CDwindow-2EDCAB04A232660E8BCBD7A079DE574B
      Page Title after second Tab Switching is : Startpage.com Search results
      Current Window Handle is : CDwindow-18CCC5501A5F68CBE1C3094D0D0B419D
      

You can find a relevant detailed discussion in Open web in new tab Selenium + Python

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352