How do you keep logged in with selenium driver? - python

I've been trying some things with selenium. However, I was annoyed that I could keep logged in google so, after some research, I found this script (from 2017) that is supposed to keep you logged in. I've tried it, and it works partially. It opens a new driver with my account logged in but then gives me this error:
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir
I-ve tried to remove the path, to change it to C:\\Users\\pablo\\AppData\\Local\\Google\\Chrome\\User Data but nothing seems to work. It eithers opens a driver without my data or one with my data but then gives me an error.
Thank you in advance for your help.
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("user-data-dir=C:\\Users\\pablo\\AppData\\Local\\Google\\Chrome\\User Data")
driver = webdriver.Chrome(chrome_options=options, executable_path="C:/Users/pablo/OneDrive/Bureau/Projets Python/Infos Insta/chromedriver")
driver.get('https://web.whatsapp.com/')
driver.quit()

Related

Stay logged in with Selenium using Chrome default profile

I want to use selenium with my Google profile, I created this code:
options = webdriver.ChromeOptions()
options.add_argument(r"user-data-dir=C:\Users\myprofile\AppData\Local\Google\Chrome\User Data") #Path to my chrome profile
driver = webdriver.Chrome(executable_path=r"C:\chromedriver.exe", options=options)
driver.get(url)
I got this error:
InvalidArgumentException: Message: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir
I also tried another profile, but return the same error:
options = webdriver.ChromeOptions()
options.add_argument('--user-data-dir=C:/Users/mxs/AppData/Local/Google/Chrome/User Data')
options.add_argument('--profile-directory=Profile 2')
driver = webdriver.Chrome(executable_path=r"C:\chromedriver.exe", options=options)
driver.get(url)
also, I tried codes in both Jupyter Notebook and VSCode
Suggestion 1
If you are okay with only one selenium process running at a time, you can use queue to achieve it. As long as you use only one process to access user-dir, it'll not cause any error.
Suggestion 2
If you are trying to stay logged in for some website, you can reuse cookies. In this case you don't need to use user-data-dir thing. Selenium will automatically create temporary user-data-dir on every run.
Suggestion 3
If you still want to use the same profile for every session, here is the hack to do it. You keep original --user-data-dir unused, instead you copy all the content of your original profile directory to some temporary directory and use it with --user-data-dir every time you initiate chrome selenium.
Suggestion
When you are trying to run your python script close all the chrome windows. I got the error
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir
when I had one of the chrome windows open. So it is better to close all the chrome windows and try again.

ChromeOption '--safebrowsing-disable-download-protection' doesn't disables the download warning in Chrome version 67.x

I am trying to use Selenium webdriver to automate some of the work. My automation includes downloading some .msg outlook email file from the web attached by somebody else. Downloading the .msg file prompted a warning from Chrome saying "This type of file can harm the computer...". Using the ChromeOptions to add argument --safebrowsing-disable-download-protection does not work, the download still prompted the warning with the argument added into the chrome options, any help will be appreciated.
Code trial:
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--safebrowsing-disable-download-protection')
driver = webdriver.Chrome(chrome_options=chrome_options)
printing the chrome_options.arguments shows that the '--safebrowsing-disable-download-protection' is added into the arguments, but when I started to download the .msg files using Selenium, I still receive the same warning.
Something to note, when i manually run chrome.exe via cmd using the '--safebrowsing-disable-download-protection', downloading without warning works.
As per your code trials as you are trying to implement --safebrowsing-disable-download-protection through ChromeOptions() but it is worth to mention the following points:
As per Remove kSbDisableDownloadProtection flag to make download safebrowsing protect a default behavior --safebrowsing-disable-download-protection is supposed to be cleaned up as a command flag to make download safebrowsing protect a default behavior.
The fix was dependent on Replace safe browsing DB and update protocol with Pver4 which was marked as fixed as no issues have been reported since full launch.
Subsequently Remove kSbDisableDownloadProtection flag to make download safebrowsing protect a default behavior was also marked as fixed.
The fix Enable PVer4 by default for desktop platforms establishes the fact that the ChromeOption --safebrowsing-disable-download-protection is no more effective.
Conclusion
As per the points mentioned above the ChromeOption --safebrowsing-disable-download-protection is no more an effective/valid ChromeOption and should be handled by PVer4 by default for desktop platforms.
You can try this:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("prefs", {
"download.default_directory": r"C:\Users\downloads",
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": False
})
driver = webdriver.Chrome(chrome_options=chrome_options)
This should work
driver = webdriver.Chrome(chromeDriver, options=options)
params = {'behavior' : 'allow', 'downloadPath':r"C:\Users\downloads"}
driver.execute_cdp_cmd('Page.setDownloadBehavior', params)

Chromedriver.exe has stopped working - python

I keep getting this error:
https://sites.google.com/a/chromium.org/chromedriver/help/chromedriver-crashes
I get it when running the command:
python Web.py
However when I go into the file and run the lines 1 by 1, I don't get the error. However I always get the error when the Web.py file has finished. When I run the lines 1 by 1, it's very basic things but i feel like I"m not ending my script correctly.
import selenium
from selenium.webdriver.common.keys import Keys
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('espn.com')
I want the window with espn.com to stay on the screen, not quit when the script has finished.
I'm running this on Python. I can share my setup, maybe that's something I did incorreclty but any help would be appeciated.
You're passing an invalid url.
You need to pass the url like this:
driver.get("http://www.espn.com")
This might work in your browser, but it won't with selenium. Type in "espn.com" in your browser and then copy / paste the url and you'll see that it's actually the above url.
You should also specify the "chromedriver.exe" path.
You are getting this error because you had not installed the chrome driver for selenium on your Machine. Selenium by default provides the driver for Firefox so when you use the webdriver for Firefox, it won't rise any error. To resolve this issue with Chrome you can download the Chrome webdriver from here.
and you can specify the driver as
from selenium import webdriver
d = webdriver.Chrome(executable_path='<your Chrome driver path>')
Adding to what #Pythonista said , it's better if you keep the URL as a raw string than a normal string
driver.get(r'http://www.espn.com')
so that it won't take the slash as an escape sequence in few cases.
Hope it helps.
Try to update chrome and get updated/latest chrome driver, recently chrome made several updates in its driver you can download the last one from the link below:
https://chromedriver.storage.googleapis.com/2.27/chromedriver_win32.zip

Webdriver issue - getting web element on Firefox browser using python is not possible

I am facing issue with Python web driver. I am able to launch the browser through webdriver and open required URL and login, but issue comes later when I try to find object of any element present on screen, it waits for long time(some times 30 min to 1 hr) to get web element. I have tried creating profile as given below, but still result is same.
fp = webdriver.FirefoxProfile()
fp.set_preference("webdriver.load.strategy", unicode("unstable")).
self.browser = webdriver.Firefox(fp)
Do any 1 know solution for this?
Note: It works fine on Chrome.
Thanks for your help..

selenium chrome attach default profile

i am using python ana selenium, to automate some process, but couldnt attached selenium to default chrome profile
i tried with,
capability = webdriver.DesiredCapabilities.CHROME
self.driver = webdriver.Remote('http://127.0.0.1:9515/wd/hib',capability)
of course, i started, chromedriver first, and also tried with,
import time
from selenium import webdriver
import selenium.webdriver.chrome.service as service
service = service.Service('./chromedriver')
service.start()
capabilities = {'chrome.binary': '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome'}
driver = webdriver.Remote(service.service_url, capabilities)
driver.get('http://www.google.com/xhtml');
time.sleep(5) # Let the user actually see something!
driver.quit()
this causes, selenium.common.exceptions.WebDriverException: Message: u'Could not find Chrome binary at:
and also tried with,
self.driver = webdriver.Chrome("./chromedriver")
this works, but not default profile, and also wonder to know, how to open new window or new tab with this ?
thanks.
Don't just copy/paste something straight off the website! Have a look into that folder yourself, does it have anything in it?! My guess is no. This is why when you leave that bit off, it works fine, because it's looking for Chrome where it should exist!
Any way, more to the point you are using it wrongly!
If you want to give Selenium a different profile to use for Chrome, then you need to use the options class:
https://code.google.com/p/selenium/source/browse/py/selenium/webdriver/chrome/options.py
You want the add_argument function.
Why?
This is because to give Chrome another profile to use, you need to launch Chrome with a specific command line (specifically --user-data-dir):
http://www.chromium.org/user-experience/user-data-directory
The add_argument function exposes the ability to add command line switches.
So if you use the add_argument function, Selenium will simply pass whatever you give it, down to Chrome as being part of it's command line switches.
To find out where your chrome profile is located start chrome and type
chrome://version
in the address bar. Under "Profile Path:" you'll see the location of the profile you're currently using. For example:
~:\Users\<username>\AppData\Local\Google\Chrome\User Data\Default

Categories