Why can chrome store this session data but firefox can not? - python

So I want to store a Whatsapp Web session in order to not having to scan Whatsapp Web's QR-Code every time. I did it with the following code:
options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=C:/Users/Pascal/AppData/Local/Google/Chrome/User Data")
browser = webdriver.Chrome(executable_path="C:/Users/Pascal/Desktop/chromedriver.exe", options = options)
browser.get("https://web.whatsapp.com/")
The above code worked perfectly (Chromebrowser) but the following code which is almost the same does not work:
options = webdriver.FirefoxOptions()
options.add_argument("--user-data-dir=C:/Users/Pascal/AppData/Roaming/Mozilla/Firefox/Profiles/iddwgmst.default-release")
browser = webdriver.Firefox(executable_path="C:/Users/Pascal/Desktop/geckodriver.exe", options = options)
browser.get("https://web.whatsapp.com/")
Why does it not work with firefox? The QR-Code comes up every time but I have loaded the firefox profile to the browser/driver so it seems like firefox does not store whatsapp webs data... But then, if I go into whatsapp web in my normal firefox browser, it stores the data again and I don't have to rescan... I am confused by this problem.
I really want it with firefox to be working cause chromedriver does not support emojis :/
Any Ideas?

I solved it.
For Firefox it works with:
profile = webdriver.firefox.firefox_profile.FirefoxProfile("C:/Users/Pascal/AppData/Roaming/Mozilla/Firefox/Profiles/iddwgmst.default-release")
self.browser = webdriver.Firefox(executable_path = os.path.dirname(os.path.realpath(__file__)) + "\\geckodriver.exe" , firefox_profile = profile)
self.browser.get("https://web.whatsapp.com/")
But for chrome it works with:
options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=C:/Users/Pascal/AppData/Local/Google/Chrome/User Data")
self.browser = webdriver.Chrome(executable_path = os.path.dirname(os.path.realpath(__file__)) + "\\chromedriver.exe", options = options)
self.browser.get("https://web.whatsapp.com/")
Here, geckodriver and chromedriver are in the same folder as the main.py

Related

How do we can use both the headless mode option and user-data-dir option?

I want to use both of the two options for screenshot sites need login.
headless mode --headless
specify user data directory --user-data-dir (set fullpath on Windows)
But this seems not to work correctly because headless driver does not hold login status.
(in the below code, I used function construct_driver and the argument headless_flg=True)
I created the user-data (and profile dir) that is read from the headless driver by creating and opening the user-data firstly from the normal driver (no headless option, login the sites, and persisted.
I confirmed that the normal driver correctly read the same profile and already logined the site from second driver run.
(in the below code, I used function construct_driver and the argument headless_flg=False)
So, I think the cause of this problem is that headless driver do not read the user-data correctly.
Is there the way to specify the user-data on headless chrome driver?
This is my code (python):
from selenium import webdriver as wd
from selenium.webdriver.chrome import service as cs
BASE_DIR = "<fullpath of the user data dir to be located>"
DRIVER_VERSION = "101.0.4951.41"
def construct_driver(headless_flg=False, profile_no=1):
csi = cs.Service(f"{BASE_DIR}/{DRIVER_VERSION}/chromedriver")
options = wd.ChromeOptions()
userdata_dir = f"{BASE_DIR}/data/Chrome-{str(profile_no)}"
if(headless_flg):
options.add_argument('--headless')
options.add_argument(f"--user-data-dir={userdata_dir}")
# profile = "1"
# options.add_argument(f"--profile-directory=Default")
driver = wd.Chrome(service=csi, options=options)
driver.implicitly_wait(10)
return driver
I am not familiar with phyton, but managed your request in C#.
There are two complete different options and the start sequence.
Maybe it will help you a little bit or give you an idea.
// hide driver Console? true/false
ChromeDriverService service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = false;
// change Standard-Download-Folder
ChromeOptions options = new ChromeOptions();
var downloadDirectory = GlobalVars.RootPath + GlobalVars.strSymbol;
options.AddUserProfilePreference("download.default_directory", downloadDirectory);
options.AddUserProfilePreference("download.prompt_for_download", false);
options.AddUserProfilePreference("disable-popup-blocking", "true");
// start Selenium Driver:
webdriver = new ChromeDriver(service, options);

How to save firefox profiles to a folder using selenium and python

When i load a firefox profile into a selenium webdriver like this:
fp = webdriver.FirefoxProfile('c:/profile_folder_path')
driver = webdriver.Firefox(executable_path='geckodriver.exe', firefox_profile=fp)
it loads the profile fine and it works. But in situation where you pass an empty folder to FirefoxProfile()
like:
fp = webdriver.FirefoxProfile('c:/empty_path')
driver = webdriver.Firefox(executable_path='geckodriver.exe', firefox_profile=fp)
it doesn't create a new profile like chrome, edge and opera.
On Chrome
options = Options()
# This will save a new profile to the path if a profile is not there
options.add_argument(f'--user-data-dir="C:/empty_path"')
It also works on Edge and Opera.
Why doesn't it work on firefox and is there any way of achieving this?

Open chromedriver in normal mode (no incognito) in selenium python in linux ubuntu? [duplicate]

I'd like to launch Chrome with its default profile using Python's webdriver so that cookies and site preferences persist across sessions.
How can I do that?
This is what finally got it working for me.
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Path") #Path to your chrome profile
w = webdriver.Chrome(executable_path="C:\\Users\\chromedriver.exe", chrome_options=options)
To find path to your chrome profile data you need to type chrome://version/ into address bar . For ex. mine is displayed as C:\Users\pc\AppData\Local\Google\Chrome\User Data\Default, to use it in the script I had to exclude \Default\ so we end up with only C:\Users\pc\AppData\Local\Google\Chrome\User Data.
Also if you want to have separate profile just for selenium: replace the path with any other path and if it doesn't exist on start up chrome will create new profile and directory for it.
This solved my problem. (remove Default at the end)
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=/home/username/.config/google-chrome")
cls.driver = webdriver.Chrome(options=options,
executable_path="./../ext/chromedriver")
Chrome_Options ist deprecated. Use options instead
I solved my problem with answer of "Yoannes Geissler".
In my case My profile was named "Profile 2"
My Code :
options = webdriver.ChromeOptions()
options.add_argument('--user-data-dir=C:/Users/GOD/AppData/Local/Google/Chrome/User Data')
options.add_argument('--profile-directory=Profile 2')
wd = webdriver.Chrome(options=options)
The below line solved my problem:
options.add_argument('--profile-directory=Profile 2')
Just to share what worked for me. Using default's profile was complicated, chrome keeps crashing.
from pathlib import Path
from selenium import webdriver
driver_path = Path("{}/driver/chromedriver75.exe".format(PATH_TO_FOLDER))
user_data_dir = Path("{}/driver/User Data".format(PATH_TO_FOLDER))
options = webdriver.ChromeOptions()
# TELL WHERE IS THE DATA DIR
options.add_argument("--user-data-dir={}".format(user_data_dir))
# USE THIS IF YOU NEED TO HAVE MULTIPLE PROFILES
options.add_argument('--profile-directory=Default')
driver = webdriver.Chrome(executable_path=driver_path, options=options)
driver.get("https://google.com/")
By doing this Chrome will create the folder User Data and keep all the data in it where I want and it's easy to just move your project to another machine.
This answer is pretty simple and self-explained.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
exec_path_chrome = "path/to/Google Chrome" #Do not use this path that is extracted from "chrome://version/"
exec_path_driver = "path/to/chromedriver"
ch_options = Options() #Chrome Options
ch_options.add_argument("user-data-dir = /path/to/Chrome Profile") #Extract this path from "chrome://version/"
driver = webdriver.Chrome(executable_path = exec_path_driver, options = ch_options) #Chrome_Options is deprecated. So we use options instead.
driver.get("https://stackoverflow.com/a/57894065/4061346")
As #MadRabbit said type chrome://version/ into the address bar to find the path to your chrome profile data.
It appears like this in Windows C:\Users\pc\AppData\Local\Google\Chrome\User Data\Default
It appears like this in Mac /Users/user/Library/Application Support/Google/Chrome/Default
So all you have to do is to erase the last portion Default from the profile path.
Note: Make sure you don't run more than one session at the same time to avoid problems.
This is what I did.
import chromedriver_binary
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
chrome_options.add_argument(r"--user-data-dir=User Data Directory")
chrome_options.add_argument(r"--profile-directory=Profile name")
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches",["enable
logging"])
chrome_options.add_experimental_option("excludeSwitches", ["enable
automation"])
chrome_options.add_argument("start-maximized")
self.driver = webdriver.Chrome(chrome_options=chrome_options)
self.wait = WebDriverWait(self.driver, 20)
Here we do not need to give the chrome driver path.

Selenium webdriver not opening websites in default chrome profile

I have tried selenium webdriver in Python and it works fine. But when I try to open default chrome profile it doesn't open the websites.
The code is
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument("user-data-
dir=/Users/prajwal/Library/Application Support/Google/Chrome")
capability = DesiredCapabilities.CHROME
capability["pageLoadStrategy"] = "normal"
driver = webdriver.Chrome(desired_capabilities=capability,
chrome_options=chromeOptions)
driver.get("https://www.google.com")
The window opens in this case. But it doesnt open the website. However, it works fine if I remove
chromeOptions.add_argument("user-data-
dir=/Users/prajwal/Library/Application Support/Google/Chrome")
Where am I going wrong ?

how can i remove notifications and alerts from browser? selenium python 2.7.7

I am trying to submit information in a webpage, but selenium throws this error:
UnexpectedAlertPresentException: Alert Text: This page is asking you
to confirm that you want to leave - data you have entered may not be
saved. ,
>
It's not a leave notification; here is a pic of the notification -
.
If I click in never show this notification again, my action doesn't get saved; is there a way to save it or disable all notifications?
edit: I'm using firefox.
You can disable the browser notifications, using chrome options. Sample code below:
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)
With the latest version of Firefox the above preferences didn't work.
Below is the solution which disable notifications using Firefox object
_browser_profile = webdriver.FirefoxProfile()
_browser_profile.set_preference("dom.webnotifications.enabled", False)
webdriver.Firefox(firefox_profile=_browser_profile)
Disable notifications when using Remote Object:
webdriver.Remote(desired_capabilities=_desired_caps, command_executor=_url, options=_custom_options, browser_profile=_browser_profile)
selenium==3.11.0
Usually with browser settings like this, any changes you make are going to get throws away the next time Selenium starts up a new browser instance.
Are you using a dedicated Firefox profile to run your selenium tests? If so, in that Firefox profile, set this setting to what you want and then close the browser. That should properly save it for its next use. You will need to tell Selenium to use this profile though, thats done by SetCapabilities when you start the driver session.
This will do it:
from selenium.webdriver.firefox.options import Options
options = Options()
options.set_preference("dom.webnotifications.enabled", False)
browser = webdriver.Firefox(firefox_options=options)
For Google Chrome and v3 of Selenium you may receive "DeprecationWarning: use options instead of chrome_options", so you will want to do the following:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
options.add_argument('--disable-notifications')
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
Note: I am using webdriver-manager, but this also works with specifying the executable_path.
This answer is an improvement on TH Todorov code snippet, based on what is working as of Chrome (Version 80.0.3987.163).
lk = os.path.join(os.getcwd(), "chromedriver",) --> in this line you provide the link to the chromedriver, which you can download from chromedrive link
import os
from selenium import webdriver
lk = os.path.join(os.getcwd(), "chromedriver",)
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(lk, options=chrome_options)

Categories