Selenium - How to import all settings from an existing Firefox profile - python

Why am I doing this:
I need to automate a website that requires client-side SSL certificates. I understand this to be an option which cannot be specified using fp.set_preference(). I am not in control of the server I am connecting to thus I cannot change the security setup.
What have I tried
I have created a separate Firefox profile which has the required 'client-side password protected SSL certificates' set up, select one certificate automaticaly and some manual proxy settings (SOCKS 5). After much googling I have set my code as follows:
from selenium import webdriver
url = 'https://www.paininneck.co.uk'
fp = webdriver.FirefoxProfile(r"""C:\Users\
<user>\AppData\Local\Mozilla\Firefox\Profiles\<Firefox>""")
driver = webdriver.Firefox(fp)
driver.get(url)
The Problem:
The browser does open, however, it is still using the default profile. None of the settings I have changed in the other profile has copied across. The profile specified in my code is still working with selecting it through the Firefox UI.
I am hoping I missed something simple and all this time googling has not been in vain! I am reluctant to change to default settings, however after tweaking the default profile to see if the settings would copy over it is apparent that they don't and Selenium is making a clean copy each time.
Kind regards
Rich
Versions:
Python==3.6.1,
Selenium==3.4.3,
Firefox==53
gecko driver==v0.16.1
OS==Windows(Its for work dont judge me!)

Using Selenium 3.4.x, Python 3.6.1 along with geckodriver v0.16.1 & Mozilla Firefox 53.0, you can use the existing Firefox profile through the following steps:
Locate the Firefox Profile directory on your windows box. For e.g. my Firefox Profile "debanjan" was located at C:\\Users\\AtechM_03\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles by the name w8iy627a.debanjan.
Next, you have to specify the absolute path of the Firefox Profile directory when you initiate the webdriver.
Here is the working code which opens an existing Firefox Profile 'debanjan' on my Windows machine:
It is to be noted that the current Selenium-Python binding is unstable with geckodriver and looks to be Architecture specific. You can find the github discussion and merge here. So you may additionally need to pass the absolute path of the firefox binary while initializing the webdriver
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
profile = webdriver.FirefoxProfile('C:\\Users\\AtechM_03\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\w8iy627a.debanjan')
binary = FirefoxBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe')
driver = webdriver.Firefox(firefox_profile=profile, firefox_binary=binary, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")
url = 'https://www.paininneck.co.uk'
driver.get(url)

Related

How to open a already logged-in chrome browser in selenium python

I want to open a browser that is logged in to my gmail account like my default browser in selenium. Is there a way to do this?
edit:
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)
this method does not work for me.
Chrome Options
Chrome options are very particular on how you call them. Syntax needs to be followed with perfection. Correct your below syntax errors by changing your old code:
options.add_argument("user-data-dir=C:\\Path")
To the following code:
userdatadir = 'C:/Users/<user>/AppData/Local/Google/Chrome/User Data'
chromeOptions.add_argument(f"--user-data-dir={userdatadir}")
You can use your profile that you use to open browser:
from selenium import webdriver
profile = webdriver.FirefoxProfile('/home/user/.mozilla/firefox/xxxx-release/') # this work for linux, if you use windows, you can find this file in local AppData
browser = webdriver.Firefox(executable_path='./geckodriver', firefox_profile =profile)
This will use your standard profile that you use with your standard browser, including logins, cookies etc.
Edit:
I used firefox here, but the same principle works with chrome as well.
options = webdriver.ChromeOptions()
options.add_argument(f"user-data-dir={CHROME_USER_DIR}")
options.add_argument("profile-directory=Default")
This will open the Default Profile in Chrome.
The user-data-dir looks something like C:\Users\<username>\AppData\Local\Google\Chrome\User Data
To open a different profile, replace the Default with your profile directory

Adding extension through selenium firefox profile success. But when I open these Firefox manually, the extension still not installed

I use selenium python. My code work success, the extension was added. But when I close the code, open the Firefox Profile which added extension by manually then the extension isn't installed.
My code
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
try:
path = "My_profile_PATH"
fp = webdriver.FirefoxProfile(path)
driver = webdriver.Firefox(firefox_profile=fp)
# path to your downloaded Firefox addon extension XPI file
extension_path = "MY_extension_PATH"
# using webdriver's install_addon API to install the downloaded Firefox extension
driver.install_addon(extension_path, temporary=True)
# Opening the Firefox support page to verify that addon is installed
driver.get("about:support")
# xpath to the section on the support page that lists installed extension
addons = driver.find_element(By.XPATH,'//*[contains(text(),"Add-ons") and not(contains(text(),"with"))]')
# scrolling to the section on the support page that lists installed extension
driver.execute_script("arguments[0].scrollIntoView();", addons)
# introducing program halt time to view things, ideally remove this when performing test automation in the cloud using LambdaTest
print("Success. Yayy!!")
time.sleep(20)
except Exception as E:
print(E)
finally:
# exiting the fired Mozilla Firefox selenium webdriver instance
driver.quit()
# End Of Script
When you ask Selenium to use a profile, it copies it to a temporary location and use this copy. So whatever you do doesn't affect the original profile.
If you want your script to affect the original profile, don't ask Selenium to use a profile. Instead, tell Firefox directly which profile to use:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.add_argument('-profile')
options.add_argument('/path/to/your/profile')
driver = webdriver.Firefox(options=options)

Firefox 102 Marionette doesn't load extensions with Selenium [Python]

I'm trying to set up any ad blocker to work with Firefox Selenium. I've set up a new profile and downloaded 3 most popular ad blockers, however when launching with Selenium none of them even show up in the Firefox extensions page. If I open Firefox normally, or if I go to about:profiles and click on the [Launch Profile in new Browser] then it shows up.
This is the python code:
# Get the path
profile_path = r'C:\Users\Sajev\AppData\Roaming\Mozilla\Firefox\Profiles\dyydkved.PythonScraper'
# Set up the webdriver options
options = Options()
options.set_preference('profile', profile_path)
# Create a webdriver object
driver = webdriver.Firefox(options=options)
driver.get(url2)
I've looked online and found this Github thread: https://github.com/mozilla/geckodriver/issues/1517
And I've tried the workaround mentioned there to lower the LastAppBuildID by 1 both by changing the number in prefs.js and in the python code like this:
options.SetPreference("extensions.lastAppBuildId", "<apppID> -1 ");
But it still doesn't work.
I also tried to make selenium install an extension on launch but fun times, that method is deprecated....

how to run Selenium ChromeDriver with cookies from actual Chrome installation?

I want to use Selenium(python) to log in to several sites and I need to get all my Chrome cookies,
How should I do this?
This is a duplicate question, anyways:
Do the following:
# Remember to import
from selenium.webdriver.chrome.options import Options
options = Options()
# Your user agent
options.add_argument("YOUR_USER_AGENT")
# Path to chrome application
options.add_argument("CHROME_APPLICATION_PATH")
# Path to chrome driver
driver = webdriver.Chrome(executable_path="CHROME_DRIVER_PATH", options=options)
You will need to do some research about your own system and see what exactly is your user-agent (there are a million site that tell you)
As well as finding the correct path for your chrome application.
Then, proceed normally, this will execute chrome as if you opened it yourself.

How to install add-on on a remote Firefox webdriver?

install_addon is a method on the Firefox webdriver, but I'm running tests remotely. How can I install an add-on to a driver like the following:
driver = webdriver.Remote(
command_executor=SELENIUM_URL,
desired_capabilities=webdriver.DesiredCapabilities.FIREFOX
)
? It looks like creating a webdriver.FirefoxProfile() and using add_extension() should work, but it's currently broken.
The use case is testing the effect of Firefox add-on manifest.json's all_frames property.
Аfter a long search and visits on this topic from google i found solution
Install addon in you Firefox profile from browser, save it and set him into webdriver options.
Firefox profile creating
Open you firefox browser
Go to about:profiles
Create a New Profile
Launch browser with created profile
Install addons what you need
Export profile
Open root directory of your firefox profile (see in in about:profiles in Root Directory section)
Copy folder into you project
Python
from selenium.webdriver import FirefoxOptions, FirefoxProfile, Remote
# init options object
options = FirefoxOptions()
# init profile object with path to you profile
profile = FirefoxProfile('{path_to_your_profile_folder}')
# set profile into options
options.profile = profile
# init you Remote browser with created options (commented arguments for example)
driver = Remote(
options=options,
# command_executor= ...,
# desired_capabilities=...
)
It`s works for me. The disadvantage of this solution is the presence of a folder with a profile that needs to be stored somewhere

Categories

Resources