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

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

Related

Is there a method for displaying a folder onscreen?

I am creating a program in Python that requires the user to place images into an Input folder, and then take images out of an Output folder. As this will become an application, the Input and Output folders will be very difficult to navigate to, being buried in the app's contents.
I am looking for a way to open folders onscreen so that a user can add or remove their own files from these folders, without knowing the exact location of the folders they are interacting with.
I am thoroughly stumped on this problem, and I appreciate all of your time.
Thank you very much.
*Edit: I am working on MacOS
If the program is intended for use on windows, it seems like you could use the solution here, where you could open explorer as a sub process and then open the path to the file.
For example
import subprocess
subprocess.Popen(r'explorer /select, FilePath')
You could also use os, and os.startfile(FilePath).

PowerPoint: Edit filepath in image pasted as object link

Note: (not sure if I should put this here or on Super User)
I paste images into PowerPoints as a link, so that it updates when I update the source. Like this:
Usually, I update the image by modifying the original and then updating the link, like this:
However, if I open it from another computer, it can't update the link because it can't find the file. I'm assuming because it uses an absolute filepath and my file is in a Dropbox-like system, so the %USERPROFILE% part of the path (like JohnDoe in C:\Users\JohnDoe\Dropbox) is different for each computer.
So I thought I could search the OOXML version of the file to see that filepath and then modify it on my own. However, I tried searching it with Python and BeautifulSoup4, but couldn't find anything.
Any idea how I can modify or find this path to the linked object?
UPDATE: I found this
answer, it worked! really helped me, thanks.

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

howoto fix working directory is always home? (python)

This is my first question.
My python script opens and reads from a present text file using the following simple funct:
open("config.ini", "r")
As this is a relative path it is supposed to work because config.ini is placed in the same directory like the script is when it is launched, that should be the current working dir.
In fact this works perfectly on all of my 3 linux boxes, but I have one user who demands support because he gets an error while opening config.ini. The error raises because
os.path.exists("config.ini")
returns false even if the file is there!
Trying to fix this problem we found out that the only way to make it work is to place config.ini in his home directory despite the supposed working directory is another.
Also, if my script tries to create a file in the present working directory, the file is always created in his home dir instead, and so I think that for some reason his working dir is always home!
How can I troubleshoot this problem? Maybe I could introduce absolute paths, but I am afraid that os.getcwd() would return the homedir instead of the correct one.
Should I maybe suggest this user to fix his machine in some way?
Sorry for this long question but english is not my first language and I am a beginner in coding, so have some difficulties to explain.
Thank you very much in advance! =)
Could it be that the user is executing your script from his home directory?
I.e. suppose the script is in:
/home/user/test/foo/foo.py
But the user calls it thus:
/home/user> python test/foo/foo.py
In this case, the "current directory" the script sees is /home/user.
What you can do is find out the directory the script itself resides in by calling this function:
import os
def script_dir():
return os.path.dirname(os.path.realpath(__file__))
It will always return the directory in which the script lives, not the current directory which may be different. You can then store your configuration file there safely.
Along the same lines as Eli Bendersky's suggestion, you might want to try:
os.path.exists(os.path.join(sys.path[0],"config.ini"))
since sys.path[0] should always be the directory in which the script resides.

Categories