Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python selenium : How to close the tab that gets open automatically along with the tab that selenium opens

I installed chrome extension on chromedriver before I open the required url. As the extension get installed, it opens a tab mentioning "Thank you for installing".

extension url:https://chrome.google.com/webstore/detail/buyhatke/jaehkpjddfdgiiefcnhahapilbejohhj?hl=en

I would like to close the tab opened by extension as it unnecessarily loads data.

code snippet:

from selenium import webdriver
import time
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from bs4 import BeautifulSoup as soup

chrome_options = Options()
chrome_options.add_argument('load-
extension=C:\\Users\\SACHIN\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Extensions\\jaehkpjddfdgiiefcnhahapilbejohhj\\3.4.141_0')

driver = webdriver.Chrome(chrome_options=chrome_options)
driver.implicitly_wait(30)

driver.get("https://paytm.com/shop/p/samsung-j3-pro-16-gb-black-MOBSAMSUNG-J3-PSTPL5914255C1B4E1C?src=grid&tracker=curated%7C%7Cmobile%20phones%7CCategory%20Grid%7C%2Fg%2Fmobile-accessories%2Fmobiles%2Fsmart-phones%7C66781%7C1%7C")
like image 513
sachin rathod Avatar asked Sep 12 '25 14:09

sachin rathod


1 Answers

You can switch to tab as

window_before = driver.window_handles[0] // May be 1 in your case 
driver.switch_to_window(window_before)

and close the driver.

Or send Alt + Tab keys and close the tab

like image 128
Bibek Adhikari Avatar answered Sep 14 '25 15:09

Bibek Adhikari