Edit Mozilla configuration file in Python - python

Settings for the Mozilla Firefox browser my be changed by entering about:config in the address bar, which will open a configuration file. I would like to be able to edit, delete and add parameters to this file in Python. How can I do this?

The configuration files used in firefox are actually a couple of javascript files that you can find under the profile directory: prefs.js and user.js.
You can use python to append lines to those files (user.js recommended) like:
user_pref(<preference>, <value>)
However, I don't really think is a good idea to use python for this.

You can check
Firefox Profiles
to get to the OS dependent profile,
also,
here is a duplicate question to point you in the right direction.
Also, maybe
this Javascript Python bridge will better assist you editing the files.

Related

What is the best way to wait for a file to appear in downloads folder - Robocorp (Python - Based)

I am using Robocorp, an RPA platform.
In my bot, I have to click a link that automatically downloads a file. (the file name is generated randomly by the site)
I then need to rename the file and move it to a specific directory.
I have two questions on the best way to do this:
Should I simply change the Chrome settings on the RPA.Browser.Selenium library to ask for the download location upon downloading. (Note that I have not been able to get this to work in Robocorp)
Or should I wait for a new file to appear in the bot's "Downloads" folder and then manage that file from there?
Above is an image of one of my many attempts of editing the 'options' argument of the Open Available Browser task to ask me where to save the file prior to downloading. I noticed there is also a 'download' argument but I do not believe it is the right one to edit based on some research.
https://robocorp.com/docs/libraries/rpa-framework/rpa-browser/keywords#open-available-browser
Here is the docs page for the "Open Available Browser' task in Selenium that shows all of its arguments.
Is there a more reliable way to do this?
This is a bit tricky scenario. First, I will start by stating I do not have any experience in Robocorp tool. But this is a generic problem in test automation and RPA.
There are few things to consider while waiting for a file to download including;
Is the file fully downloaded?
What is the max time I need to allow for download?
What if there are pervious files in the folder with similar name?
So to overcome this I would take the following approach. (tested and working for more than 4+ prod level automation frameworks with >20k scenarios)
Maintain a unique "Downloads" folder for each execution. Do this by creating a new folder with unique name and setting that as the downloads folder at the beginning of the run.
In a loop, check the download file size continuously and wait for the file size to NOT increase to check the file is fully downloaded.
??? profit ???

Internet Shortcut in python

I have a problem. Let's say I have a website (e.g. www.google.com). Is there any way to create a file with a .url extension linking to this website in python? (I am currently looking for a flat, and I am trying to save shortcuts on my hard drive only to apartment offers posted online matching my expectations ) I've tried to use the os and requests module to create such files, but with no success. I would really appreciate the help. (I am using python 3.9.6 on Windows 10)
This is pretty straightforward. I had no idea what .URL files were before seeing this post, so I decided to drag its URL to my desktop. It created a file with the following contents which I viewed in Notepad:
[InternetShortcut]
URL=https://stackoverflow.com/questions/68304057/internet-shortcut-in-python
So, you just need to write out the same thing via Python, except replace the URL with the one you want:
test_url = r'https://www.google.com/'
with open('Google.url','w') as f:
f.write(f"""[InternetShortcut]
URL={test_url}
""")
With regards to your current attempts:
I've tried to use os and requests module to create such file
It's not clear what you're using requests or os for, since you didn't provide a Minimal Reproduceable Example of what you'd tried so far; so, if there's a more complex element to this that you didn't specify, such as automatically generating the file while you're in your browser, or something like that, then you need to update your question to include all of your requirements.

Google Chrome cannot read and write to its data directory : selenium

Here's the issue i am facing now.
I could launch chrome driver. However my selenium code suddenly doesnt work and pops up above image.
Hope someone can shed light as i couldn't find a solution online. .
Generally BarthRid's answer is correct, providing full path to userdata folder works with Chrome 90+. But if you store userdata folder in the same directory as your script, you can use pathlib to manage things more easily.
import pathlib
script_directory = pathlib.Path().absolute()
options.add_argument(f"user-data-dir={script_directory}\\userdata")
This way allows you to store actual script's directory in a variable, and via formatted string put it in selenium's options argument. Useful while dealing with multiple instances.
I had a similar problem to yours and #scott degen.
options.add_argument line of your pasted screenshot
I changed mine from:
options.add_argument("user-data-dir=selenium")
to the full directory, and it seems to be working better now.
options.add_argument("user-data-dir=C:\environments\selenium")
I changed mine from:
options.add_argument("user-data-dir=selenium")
to the full directory, and it seems to be working better now.
dir_path = os.getcwd()
chrome_option.add_argument(f'user-data-dir={dir_path}/selenium')
It seems Chrome changes its Current Working Directory during startup and so the relative path won't work. You can try to convert the user-data-dir to absolute path then pass to chrome. See Bug: 1058347

How to change chrome's default download path with selenium?

My scenario is: I had to write a script to download a huge amount of files by using Selenium and Python 3.6, and now, I have to download files using the same technologies.
The point is that this script won't be executed on my own computer.
Is it possible, using chrome webdriver, to get the default download folder of chrome?
As of now I have this code:
dlPth="C:\\Users\\genieelecpsim\\Downloads\\"
nwPth="C:\\Users\\genieelecpsim\\Downloads\\Exports"
for file in os.listdir(dlPth):
if file.startswith("export") and file.endswith(".csv"):
print(str(years[i])+"-"+str(months[j])+"-"+str(days[k]))
newfile=os.path.join(nwPth,str(years[i]) +"-" +(str(months[j]) if months[j]>=10 else "0"+str(months[j]))+"-" +(str(days[k]) if days[k]>=10 else "0"+str(days[k])) +".csv")
shutil.move(os.path.join(dlPth,file),newfile)
print (newfile)
break
What I want to do here is something like:
dlPth=# Chrome's default download directory
nwPth=dlPth+"\\Export"
Is it possible? Thanks for your response!
EDIT: First of all, thanks to all for your quick answers, and it seems that my topic's a dupplicate, but as I'm not using the same configuration than this one, I'm wondering if this method works with py3.6 and Selenium 3.0.2... I'm sorry I can't directly comment your answers as I'm new here, but thanks to everyone!
You can change download folder by using specific preferences when starting your driver. You should set this:
("download.default_directory", yourWantedPath)
Not sure how are you starting and configuring your driver so can't help you with more code, but this is the preference you are looking for.
You could find useful things here.
The answers did not really help me, but thanks anyway. I find another way to do so by using the OS library:
# dlPth will be the path to the download directory of the current user (on the system)
dlPth=os.path.join(os.getenv('USERPROFILE'), 'Downloads')
# destPth will just be a directory where I'll put all my (renamed) files in.
destPth=dlPth+"\\Exports\\"
Thanks for answering, I posted the answer here so anyone looking for some help on this topic will be able to see it

how to enable firefox addons in Ghost.py

Is it possible to run imacro firefox script inside ghost.py?
I want to automate heavy ajax sites.
I'm trying this:
from ghost import Ghost
ghost = Ghost(plugins_enabled=True,plugin_path=['C:\Documents and Settings\my\Desktop\addons\addon-3863-latest.xpi'],)
Within Ghost.py you would find the code snippet:
if plugin_path:
for p in plugin_path:
Ghost._app.addLibraryPath(p)
From the look of things, it's expecting a path (addLibraryPath) and would do its discoveries by itself. So, give it a path containing the .xpi
Note: this isn't tested.
On my Windows 10, I typed this into a Windows address bar:
%APPDATA%\Mozilla\Firefox\Profiles
I then saw a folder named "kswjuot9.default" (it might be named another things on your PC) and clicked it. Finally, I found a folder named "extensions".
Try to give Ghost.py the full address of this "extensions" folder. I would also recommend that you use forward slashes e.g.
C:/Users/iChux/AppData/Roaming/Mozilla/Firefox/Profiles/kswjuot9.default/extensions
I saw an online link on dealing with how to extract the .xpi file

Categories