Anil Kumar
Anil Kumar

Reputation: 1

How to find the list of tab names in actively opened browser using python script

I want to list down the all tab titles in already opened browser.

I have used Python and selenium webdriver package but it is creating a new browser session and listing out the tab names instead of listing from already opened browser

I have tried with Python 3.7 version and selenium package

import selenium.webdriver as webdrive
path=r"C:\Windows\chromedriver"
driver=webdriver.Chrome(executable_path=path)
browser.get('https://www.google.com?q=python#q=python')
length=len(driver.window_handles)
titles=[]
for i in range(length):
driver.switch_to.window(driver.window_handles[i])
titles.append(driver.title)
print(title)

I am expecting the output in already opened browser not from newly created one.

Upvotes: 0

Views: 2572

Answers (4)

Alfred.37
Alfred.37

Reputation: 159

Alternate to your asked way by python, you can do it:

by copy and past by hand
by Brotab
by Selenium
by xdotool
by javascript from serversite or by code injection from client side

Upvotes: 0

Alfred.37
Alfred.37

Reputation: 159

One alternate way is to get the titles of browser tab by bash on follow way:

browser=Firefox
page_title=$(wmctrl -l -p | grep "$browser")
echo "$page_title"

Upvotes: 0

Alfred.37
Alfred.37

Reputation: 159

The follow give you the title of most active tab of the first or only one opened firefox browser, by python.

import subprocess  
wins = subprocess.run('wmctrl -l', shell=True, stdout=subprocess.PIPE)
title = next(ln for ln in wins.stdout.decode('utf-8').splitlines() if 'Mozilla Firefox' in ln)
print title

Upvotes: 1

sayhan
sayhan

Reputation: 1184

It is impossible to do. There is already couple of opened issues about this feature in selenium's github page and all of them are closed.

Example issue

Upvotes: 0

Related Questions