11

I need to get all urls from all open Google Chrome tabs using python 3 without intefering with the user. My operating system is Windows 10 and I use Microsoft Visual Studio Python3.

I have tried the following things:

Opening the tabs directly using open(path_to_current_tabs). This doesn't work because I don't have permission to read the file. I think that it's locked because chrome is actively writing to it.

Current_Tabs_Source = open(r"C:\Users\Beni\AppData\Local\Google\Chrome\User Data\Default\Current Tabs", "r")
Current_Tabs_Raw = Current_Tabs_Source.read()
print(Current_Tabs_Raw) #just for checking 

The resulting error:

PermissionError: [Errno 13] Permission denied

Opening using the history SQlite3 database also doesn't work because it's locked. But I can't find a password anywhere. I have also tried to open the History for the urls, but that didn't work either.

import sqlite3
from os import path

data_path = path.expanduser('~') + r"\AppData\Local\Google\Chrome\User 
Data\Default"
files = listdir(data_path)
history_db = path.join(data_path, 'history')

c = sqlite3.connect(history_db)
cursor = c.cursor()
select_statement = "SELECT urls.url, urls.visit_count FROM urls, visits 
WHERE urls.id = visits.url;"
cursor.execute(select_statement)

results = cursor.fetchall()

print(results) #just for checking

This results in the following error:

sqlite3.OperationalError: database is locked

Using selenium and a third party chrome extension to copy all urls to the clipboard didn't work either, because these extensions only work in the active selenium window. So the links of the tabs that I want don't get copied.

I have considered hacking together a chrome extension that copies the urls every 30 sec to a temp file, but my javascript knowledge is minimal, so that thing is driving me mad.

Does anyone know a way to do this in Python? Any other solution would be greatly appreciated.

9
  • I can't think of any possible way this can be accomplished in Python.. I don't even know how you attempted this with SQLite, a database language... Your best bet would indeed be something more front-end, like JavaScript. Commented Dec 27, 2017 at 17:19
  • 1
    It would be helpful if you can share the code you've worked with up til now so we can better understand your approach Commented Dec 27, 2017 at 17:24
  • 1
    @HamSam i uploaded some code so you can see what im trying Commented Dec 27, 2017 at 18:33
  • Have you tried running it as an administrator? That might give you a different error from 'PermissionError: [Errno 13] Permission denied' Commented Dec 27, 2017 at 19:26
  • @tww0003 Just checked if this works. I opened Visual Studio as Admin and ran my code but it still gives me the exact same error. Commented Dec 27, 2017 at 19:31

3 Answers 3

1

If you want to access the database, you should close all browsers.

(Source)

Sign up to request clarification or add additional context in comments.

Comments

1

When we're accessing the SQLite database of any browser, we have to close that particular browser first.

Moreover, the SQL command being used here will fetch all the duplicate rows.

Need to change `select_statement'

select_statement = "SELECT distinct urls.url, urls.visit_count FROM urls, visits WHERE urls.id = visits.url;"

Further, we need to add a loop to print all the 'urls' from history database of chrome.

for url, count in results:
    print(url)  # print urls line by line

However, this will give us the whole history of the Chrome browser but not the required URLs of all the presently opened tabs.

Comments

0

You can use windows shadow copy service to make copy of locked sqlite database. And then read it normally.

There is python module for that https://github.com/sblosser/pyshadowcopy

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.