How to save firefox profiles to a folder using selenium and python - 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?

Related

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

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

How to open a specific chrome profile and interact with it on python

I need a script that opens a specific chrome profile and interacts by opening web pages using that profile. From another post on stackoverflow I found this code which in my case opens a different profile from the one I want.
This is the code
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("user-data-dir=C:\\Users\\giaco\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 1")
driver = webdriver.Chrome(executable_path=r'D:\chromedriver_win32\chromedriver.exe', options=options)
driver.get("https://www.google.co.in")
the profile I would like to use is to this path
but the profile that is actually opened is this and it is not the profile I want but as if it were a guest profile
how can I use the profile I want?

Save settings for extentions in Chromedriver using selenium with python

I'm using chromedriver with selenium for python.
I would like to save some settings for an extention, so that the settings come back everytime I load chromedriver with that extention. See below, I would like to save username and password
I'm currently loading extentions as below.
def __init__(self):
capa = DesiredCapabilities.CHROME
capa["pageLoadStrategy"] = "none"
chrome_options = Options()
chrome_options.add_extension('/home/simonsays/PycharmProjects/lbc_piscine_spider/chrome_extentions/Cookie-AutoDelete_v2.1.2.crx')
chrome_options.add_extension('/home/simonsays/PycharmProjects/lbc_piscine_spider/chrome_extentions/Random-User-Agent_v2.1.10.crx')
chrome_options.add_extension('/home/simonsays/PycharmProjects/lbc_piscine_spider/chrome_extentions/Proxy-Auto-Auth_v2.0.crx')
self.driver1 = webdriver.Chrome("/var/chromedriver/chromedriver", desired_capabilities=capa,chrome_options=chrome_options)
I resolved my issue with profile approach :
capa = DesiredCapabilities.CHROME
capa["pageLoadStrategy"] = "none"
chrome_options = Options()
chrome_options.add_argument(
"user-data-dir=/home/simonsays/PycharmProjects/lbc_piscine_spider/chrome_extentions/profile")
chrome_options.add_argument("--profile-directory=test")
self.driver = webdriver.Chrome("/var/chromedriver/chromedriver", desired_capabilities=capa
When the driver opens, I install whatever extensions I want, and they get saved in the profile folder I defined above. I guess you could also copy whatever existing profile you want to use.

How do you use credentials saved by the browser in auto login script

When I manually open a browser, both firefox and chrome, and proceed to a website where I previously saved my login credentials via the browser, the username and password fields automatically populate as they should. However, when I use the python selenium webdriver to open the browser to a specific page, the fields do not populate.
The point of my script is to open the webpage and use element.submit() to login since the login credentials should already be populated., but the are NOT.
How can i get them to populate in the fields?
For Example:
driver = webdriver.Chrome()
driver.get("https://facebook.com")
element = driver.find_element_by_id("u_0_v")
element.submit()
This is because selenium doesn't use your default browser instance, it opens a different instance with a temporary (empty) profile.
If you would like it to load a default profile you need to instruct it to do so.
Here's a chrome example:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
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)
And here's a firefox example:
from selenium import webdriver
from selenium.webdriver.firefox.webdriver import FirefoxProfile
profile = FirefoxProfile("C:\\Path\\to\\profile")
driver = webdriver.Firefox(profile)
Here we go, just dug up a link to this in the (unofficial) documentation. Firefox Profile and the Chrome driver info is right underneath it.
download chromedriver.exe: https://chromedriver.chromium.org/downloads
show chrome version and profiles: open chrome and type: "chrome://version/" on URL
from selenium import webdriver
from os.path import abspath
from os import path
from time import sleep
options = webdriver.ChromeOptions()
options.add_argument(r"--user-data-dir=C:\...") # Path to your chrome profile or you can open chrome and type: "chrome://version/" on URL
chrome_driver_exe_path = abspath("./chromedriver_win32/chromedriver.exe") # download from https://chromedriver.chromium.org/downloads
assert path.exists(chrome_driver_exe_path), 'chromedriver.exe not found!'
web = webdriver.Chrome(executable_path=chrome_driver_exe_path, options=options)
web.get("https://www.google.com")
web.set_window_position(0, 0)
web.set_window_size(700, 700)
sleep(2)
web.close()
If it navigates to "data/default" you can put your new url in that tab by doing driver.switch_to_window(driver.window_handles[0]).
options.add_argument("user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data\\Profile num\\")
path = os.path.abspath(filedialog.askopenfile().name)
driver1 = webdriver.Chrome(executable_path=path,options=options)
before you do this open chrome and add a profile then go into the profile and paste the contents of the profile into the default dir

selenium web driver does not open firefox with custom prefrences

Trying to open firefox with already installed addons using selenium 2 but always opens with default firefox profile having predefined preferences
from selenium import webdriver
driver = webdriver.Firefox()
The above lines of code initiates firefox with default profile.
How to make it initiate with user specified preferences?
You can launch it with a custom profile using something like:
profile = FirefoxProfile("path.to.profile")
driver = webdriver.Firefox(profile)
There is no need to set profile, it could be created automatically, you just need path to addons, e.g.:
string firebugPath = "C:\\Users\\Administrator\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\x7nutq33.default\\extensions\\firebug#software.joehewitt.com.xpi";
FirefoxProfile profile = new FirefoxProfile();
profile.AddExtension(firebugPath);
Driver = new FirefoxDriver(profile);

Categories