How to access Google Chrome Extensions (Session Buddy) via Python? - python

I want to access Session Buddy using Python.
In my case "access" means to get all currently opened URLs from Chrome.
Session Buddy allows you to save all opened URLs into a .csv file.
To do so you need to "setup" a few things (simplified: press buttons) and then all URLs are downloaded to Chromes /Downloads directory.
I would like to fully automate this process though. This means python needs to access Session Buddy, initiate the download and then save the file to the directory you want it to.
I can't use requests or something though since an extension won't work using an URL. This is what the extension calls: chrome-extension://edacconmaakjimmfgnblocblbcdcpbko/main.html
In general, I don't necessarily want to use Session Buddy to get all the URLs, it just seems to be the easiest way..
So, in summary, I just want to ask: How can I automatically use Python to fetch all currently opened URLs in my Chrome Browser (using Session Buddy)?
I'm thankful for any kind of help.

You can use selenium webdriver and load .crx(extension) file to automate
chrome_options = Options()
chrome_options.add_extension('path_to_extension')
driver = webdriver.Chrome(executable_path=executable_path,chrome_options=chrome_options)
driver.get("http://stackoverflow.com")
driver.quit()

Related

How to open browser with all credentials and settings already loaded

Hope everyone is doing okay 😊
Currently I’m trying to get info from a page where I have to sign in and then complete a Captcha (selecting different pictures) in order to proceed. The thing is that when I enter the page from my default browser (like manually clicking chrome) I don’t need to sign in and complete the captcha, because I am already signed in (I think because of the cookies?).
Every time I run selenium it opens a “blank” Chrome, is it possible to open a browser with all my data already loaded in order to skip the captcha?
I have tried different solutions explained here but with no success How to save and load cookies using Python + Selenium WebDriver
Thank you very much!
Yes there is a way: You can load your Chromebrowser with the chrome-profile saved on your computer. To do this you have to use webdriver.Chromeoptions().
options = webdriver.ChromeOptions()
options.add_argument(r'--user-data-dir=C:\Users\YourUser\AppData\Local\Google\Chrome\User Data\')
PATH = "/Users/YourUser/Desktop/chromedriver"
driver = webdriver.Chrome(PATH, options=options)
In your chrome-user, every cookie and stuff is saved and you no longer have a clean browser. Remember: There are still websites which can detect that your using selenium, but there are only a few...

Decryption problem when using linux cookies on windows

When I try to use Cookies file (sqlite fomatted) on Linux (Ubuntu) and Windows I fall into troubles with decryption of 'encrypted_value'. Is there any chance to make Cookies file compatible with two systems?
Basically selenium driver use Cookies file for various purpouses, and everything works on Linux. Sometimes my action is needed, so I want to have this Cookies file on my desktop which works on Windows, but when I download it directly and copy-paste it to my profile directory, my chromedriver logs error:
[4708:4884:0604/082853.607:ERROR:os_crypt_win.cc(61)] Failed to decrypt: The parameter is incorrect. (0x57)
I assume that there is some problem with 'encrypted_value' column decryption but I am unable to work-around this issue.
I use selenium for python this is snippet where I create options for my webdriver:
def create_options_for_webdriver(session_directory):
print('Creating options for webdriver!')
options = Options()
options.add_argument("user-data-dir=my_userdir")
options.add_argument("user-agent=my_useragent")
options.add_argument('--disable-background-networking ')
options.add_argument('--disable-client-side-phishing-detection')
options.add_argument('--disable-default-apps')
options.add_argument('--disable-hang-monitor')
options.add_argument('--disable-popup-blocking')
options.add_argument('--disable-prompt-on-repost')
options.add_argument('--disable-sync')
options.add_argument('--disable-web-resources')
options.add_argument('--enable-automation')
options.add_argument('--enable-blink-features=ShadowDOMV0')
options.add_argument('--force-fieldtrials=SiteIsolationExtensions/Control')
options.add_argument('--ignore-certificate-errors')
options.add_argument('--no-first-run')
options.add_argument('--password-store=basic')
options.add_argument('--use-mock-keychain')
return options
before option creating I create minimal directory structure which looks like my_userdir/Default/, and I download Cookies file to Default folder.
... when I download it directly and copy-paste it to my profile directory, my chromedriver logs error:
[... ERROR:os_crypt_win.cc(61)] Failed to decrypt: The parameter is incorrect. (0x57)
It looks like it is not possible. Or maybe not possible they way you are trying to do it. It takes extra effort.
The question Decrypting Chrome's cookies on windows has a link to os_crypt_win.cc. os_crypt_win.cc uses DPAPI, which is the old WinCrypt gear. DPAPI ties encryption to a user's Windows login. DPAPI also places a MAC on the encrypted data. I believe the MAC is the reason for the message you are seeing: "The parameter is incorrect". DPAPI sees the MAC over the encrypted data is wrong, and it gives you the generic error message.
So if you truly want to use the Linux cookie on Windows, you will need to decrypt it using the Linux spec, and then re-encrypt it using the Windows spec.
If you are going to pursue it, then you may want to visit this BlackHat talk: Reversing dpapi and stealing windows secrets offline. It will allow you to encrypt the user's data on Linux for Windows.
Coming back with update!
It appears that, as #jww mentioned process is more complicated, but just a little :)
In order to make Cookies fully compatible with any OS some special treatment must be applied.
In my case I used pickle library to create compatible file. To achieve it, things needs to be done as follow:
from selenium.webdriver import Chrome
import pickle
driver = Chrome()
####here you do some job which generate cookies like FB login or whatever
input("Press any key to close session") #you cant simply close browser, in order to make it work browser have to be closed in console so the rest of script will execute
pickle.dump(driver.get_cookies(), open('cookies.pkl',"wb"))
driver.quit()
print("Session closed!")
This will create cookies.pkl file which can be accessed under any OS like that:
import pickle
from selenium import Chrome
driver = Chrome()
for cookie in pickle.load(open("cookies.pkl"."rb")):
driver.add_cookie(cookie)
### anything you want to execute
As I mentioned this kind of cookies file will work under any OS, sadly it forced me to use selenium, but better this than nothing :)

Open URL in Python in existing tab

I'm using webcommands to control a Sonoff. To change the setting I run the following line in Python:
webbrowser.open('http://Sonoff_IP/cm?cmnd=POWER%20TOGGLE')
I am looking for a way to run the URL in the same tab, so as not to create a new tab every time the command runs.
My understanding is that using if you are using webbrowser.open(<url>) it's not possible to avoid getting a new tab each time; with webbrowser it is possible to make sure it opens in the same browser window, but not in the same tab. To target the same window you need to set new=0 like:
webbrowser.open('http://Sonoff_IP/cm?cmnd=POWER%20TOGGLE', 0);
However, if you are able to open the link using the selenium library instead it is possible.
Read the docs for selenium and webdriver here: https://selenium-python.readthedocs.io/api.html
The main issue with doing it using Selenium is that, I think, you lose the ability to target the user's default web browser, and by default Selenium seems to default to Firefox since a lightweight port of Firefox is included in the Selenium library itself.
An example of opening a link in Selenium would be like:
from selenium import webdriver
link1="https://www.google.com"
link2="https://www.youtube.com/"
driver=webdriver.Firefox()
driver.get(link1)
driver.get(link2)
Selenium does support a lot of different browsers, so if you are able to get the user's default web browser from the webbrowser module or by some other method, you would be able to use that information to open URLs in the same tab with the user's default browser.
Hope this helps and good luck! :)
Use Javascript:
OpenSameTab = '<script language="JavaScript" type="text/JavaScript">window.location = \'%s\';</script>'
and then
print OpenSameTab % 'file.py'

how to get list of browser installed in a system using python?

I would like to write a script (in python) which scans the machine.(assumption is system has Linux) and retrieve the list of browser's installed. Looking for suggestions to implement it. I am using selenium to open links
browser = webdriver.Firefox()
Here we have to mention Firefox to open link in Firefox browser. What if user don't have Firefox in machine (chrome installed)?
I had already searched but haven't got any result.
P.S: If system has windows/Mac OS
A better approach: use try/except block
try:
browser = webdriver.Firefox()
browser.get('url')
except (IOException, Exception):
pass
It will help in cases where driver is not able to find the browser or there is some problem while launching.
You can scan all .desktop files in /usr/share/applications looking for WebBrowser in Categories field.

Auto saved password is not working when a URL is open through python script

I wrote a code in which multiple URL is opening in Mozilla browser. But all URL have required login details. To avoid login code in my script, I used 'Saved Password' tactics. Before running to script I opened all URL and fill credential and marked as saved password. Assuming when I will run script then It would not ask any login. Unfortunately this is not working. When I am running script then login is required at that time too.
Please suggest where I am missing.
Every time a selenium-powered browser is fired up - it starts with a complete clean session by default. What you save and configure in your actual browser manually would not automagically be applied there.
What you need to do is to let your driver know that you want to use an existing profile via FirefoxProfile. Here is a great HOWTO.
In short: locate the existing profile directory and point your FirefoxProfile instance to it:
profile = webdriver.FirefoxProfile('/path/to/profile/directory')
driver = webdriver.Firefox(profile)

Categories

Resources