Python Firefox Selenium Load Profile - python

I tried many combinations of Geckodrive,Selenium and Python but i could not achieved to load Firefox profile what i need.It loads the default profile.
The lastest code is below and I opened an issue on github Selenium page but there is still no solution.There are some solutions about Java but I am not able to bind it in Python.A temp file created on temp folder which includes profile data and cookies.That mean selenium does not use any profile's cookies and other configurations
Any solutions will be apprecited.
Geckodrive version:0.23
Selenium 3.14
Python 3.7
Firefox 61
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.support.ui import WebDriverWait
fp = FirefoxProfile('C:\\Users\\<USER>\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\ji8rbij6.test')
fp.update_preferences()
browser = webdriver.Firefox(firefox_profile=fp)
Thanks in advance
PS. I tried this code block in 3 different computer because of being able to have a problem on my own computer

This exact setup works as expected for me (windows10) and I'm able to load custom firefox profiles.
Firefox: 88.0.1
Geckodriver: 0.30.0 (geckodriver releases
Python: 3.8.10
robotframework-selenium2library: 3.0.0
robotframework-seleniumlibrary: 6.0.0
import SeleniumLibrary
from selenium import webdriver
from robot.libraries.BuiltIn import BuiltIn
def create_profile(self)
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList", 2)
fp.set_preference("browser.download.manager.showWhenStarting", False)
fp.set_preference("browser.download.panel.shown", False)
fp.set_preference("browser.download.dir","**provide_download_dir_here**")
fp.set_preference("browser.helperApps.neverAsk.openFile",
"application/zip,application/octet-stream, application/x-zip-compressed,
multipart/x-zip,image/jpeg,application/xml,application/pdf,text/plain,text/csv,
*/*")
fp.set_preference("browser.helperApps.neverAsk.saveToDisk",
"application/zip,application/octet-stream, application/x-zip-compressed,
multipart/x-zip,image/jpeg,application/xml,application/pdf,text/plain,text/csv,
*/*")
fp.update_preferences()
return fp.path
# Add the following code where you open the browser
ff = self.create_profile()
BuiltIn().get_library_instance("SeleniumLibrary").open_browser("https://your_url","F irefox",ff_profile_dir=ff)

Related

How to use a existing chrome installation with selenium webdriver?

I would like to use an existing installation of chrome (or firefox or brave browser) with selenium. Like that I could set prespecified settings / extensions (e.g. start nord-vpn when opening a new instance) that are active when the browser is opened with selenium.
I know there is selenium.webdriver.service with the "executeable-path" option, but it doesn't seem to work when you specify a specific chrome.exe, the usage seems to be for the chrome-driver only and then it still opens a "fresh" installation of chrome.
Starting selenium with extension-file I think is also not an option to use with the nord-vpn extension, as I have two-factor authentication active and login every single time would take too much time and effort, if possible at all.
Firefox profile
To use the existing installation of firefox you have to pass the profile path through set_preference() method using an instance of Option from selenium.webdriver.common.options as follows:
from selenium.webdriver import Firefox
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options
profile_path = r'C:\Users\Admin\AppData\Roaming\Mozilla\Firefox\Profiles\s8543x41.default-release'
options=Options()
options.set_preference('profile', profile_path)
service = Service('C:\\BrowserDrivers\\geckodriver.exe')
driver = Firefox(service=service, options=options)
driver.get("https://www.google.com")
You can find a relevant detailed discussion in Error update preferences in Firefox profile: 'Options' object has no attribute 'update_preferences'
Chrome profile
Where as to use an existing installation of google-chrome you have to pass the user profile path through add_argument() using the user-data-dir key through an instance of Option from selenium.webdriver.common.options as follows:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
options = Options()
options.add_argument("user-data-dir=C:\\Users\\username\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
driver.get("https://www.google.com/")
You can find a relevant detailed discussion in How to open a Chrome Profile through Python

Selenium firefox profile not working anymore

So I was happily using selenium together with Firefox and it seems that my firefox profile wouldn't load anymore some morning. It is driving me nuts to be honest. Whatever I try, the profile that is being used keeps being in the temp folder.
The following snippet is what I am doing
def getFireFoxBrowserWithUserFolder(folder) :
options = webdriver.FirefoxOptions()
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_argument("user-data-dir=" + folder)
firefox = webdriver.FirefoxProfile(folder)
return webdriver.Firefox(options=options, executable_path=r'/home/bunsen/seleniumDriver/geckodriver', firefox_profile=firefox)
This worked up until very recent.
I got the newest version of the geckodriver(0.31.0), running it all on Debian stable, so my FF version is 91.10.0ESR (that is also what I see in the selenium browser).
I am at a loss at this point, anyone with the same problem?
first install webdriver_manager:
pip install webdriver-manager
then:
from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver import Firefox
def getFireFoxBrowserWithUserFolder(folder) :
options = webdriver.FirefoxOptions()
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_argument("user-data-dir=" + folder)
# here you can choose driver version
driver = Firefox(options = options, executable_path=GeckoDriverManager(version="2.26").install())
return driver
More informations about webdriver manager here.

FireFox keeps asking, "What should Firefox do with this file?" What's the about:config setting in need to change to autodownload?

I'm trying to auto download documents with Firefox via Selenium and Python. I've made some attempts at stopping the automatically generated box but Firefox keeps asking, "What should Firefox do with this file?". What's the about:config setting I need to change or add to the Firefox profile so that I don't get this prompt box? My existing code is below:
""
autodlprofile = webdriver.FirefoxProfile()
autodlprofile.set_preference("browser.download.folderList", 2)
#autodlproflie.set_preference("browser.download.alwaysOpenInSystemViewerContextMenuItem", False)
autodlprofile.set_preference("browser.download.dir", r"C:\Users\X\Desktop\Automate the Boring Stuff\taxbills")
autodlprofile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream")
autodlprofile.set_preference("browser.helperApps.neverAsk.openFile", "application/octet-stream")
autodlprofile.set_preference("browser.helperApps.showOpenOptionForPdfJS", False)
""
Thank you for your time.
This setting is tested and works as expected (I tested it with firefox version 88.0.1, geckodriver 0.30.0 and Python 3.8.10.
Important Note: be careful if you will use very recent Firefox versions (> 90), I think there might be isssues with the automation of download features).
import SeleniumLibrary
from selenium import webdriver
from robot.libraries.BuiltIn import BuiltIn
def create_profile(self)
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList", 2)
fp.set_preference("browser.download.manager.showWhenStarting", False)
fp.set_preference("browser.download.panel.shown", False)
fp.set_preference("browser.download.dir","**provide_download_dir_here**")
fp.set_preference("browser.helperApps.neverAsk.openFile",
"application/zip,application/octet-stream, application/x-zip-compressed,
multipart/x-zip,image/jpeg,application/xml,application/pdf,text/plain,text/csv,
*/*")
fp.set_preference("browser.helperApps.neverAsk.saveToDisk",
"application/zip,application/octet-stream, application/x-zip-compressed,
multipart/x-zip,image/jpeg,application/xml,application/pdf,text/plain,text/csv,
*/*")
fp.update_preferences()
return fp.path
# Add the following code where you open the browser
ff = self.create_profile()
BuiltIn().get_library_instance("SeleniumLibrary").open_browser("https://your_url","F irefox",ff_profile_dir=ff)

Python : Downloading file from the link which is implemented with php

I am trying to download file from the web page.
The link of file is implemented by php:~/download.php?id=~
The download of a file is possible to click the link or right-click and select the menu, "save this file" in the web browser.
At first, I used the selenium with phantomjs. It was successful to get the link with tag "a" by "find_element". I performed clicking or right-clicking with ActionChains of selenium, but it couldn't download the file. By searching the web, it looks like phantomjs doesn't support the download of a file.
What I consider to use as second way is using firefox or chrome which looks like supporting downloading file. Please give me an advice whether this way is the best or not. I am running the program on raspberry pi b+.
Thank you very much.
The easiest way to download file:
import urllib
url = "http://domain.com/~/download.php?id=~"
path_to_file = "/local/folder/where/you/want/to/save/file/file_name"
Python 2.x
urllib.urlretrieve(url, path_to_file)
Python 3.x
urllib.request.urlretrieve(url, path_to_file)
If you need to download file with selenium:
Firefox
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
profile = FirefoxProfile ()
profile.set_preference("browser.download.folderList",2)
profile.set_preference("browser.download.manager.showWhenStarting",False)
profile.set_preference("browser.download.dir", '/download/folder/by/default')
profile.set_preference("browser.helperApps.neverAsk.saveToDisk",file_MIME_type)
driver = webdriver.Firefox(firefox_profile=profile)
Chrome
from selenium import webdriver
download_dir = "/download/folder/by/default"
chrome_options = webdriver.ChromeOptions()
preferences = {"download.default_directory": download_dir ,
                      "directory_upgrade": True,
                      "safebrowsing.enabled": True }
chrome_options.add_experimental_option("prefs", preferences)
driver = webdriver.Chrome(chrome_options=chrome_options)

Download file from sharepoint using selenium webdriver python

I am trying to download file from sharepoint url and written code to neverask.savetodisk but still it is showing dialog to save file. I tried same code and it works when we click download link from other URL but not working with sharepoint application. Here is code what i used...
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
# To prevent download dialog
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2) # custom location
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference("browser.download.defaultFolder",'tt_at');
profile.set_preference("browser.download.lastDir",'tt_at');
profile.set_preference('browser.download.dir', 'tt_at')
profile.set_preference("browser.download.useDownloadDir",True);
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', "application/octet-stream,application/msexcel")
browser = webdriver.Firefox(profile)
browser.get("https://docs.ad.sys.com/sites/cloud/Project/Form/FolderCTID=0x01200069047C40C93C3846B74E0776AAD1610A&InitialTabId=Ribbon%2EDocument&VisibilityContext=WSSTabPersistence")
browser.find_element_by_xpath('/html/body/form/div[8]/div/div[3]/div[3]/div[2]/div/div/table/tbody/tr/td/table/tbody/tr/td/div/table[1]/tbody/tr/td/table/tbody/tr[12]/td[4]/div[1]/a').click()
but this above code still showing dialog to select location.
I think I got the solution, try the following:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
profile = webdriver.FirefoxProfile()
#Give Complete Path of download dir
profile.set_preference("browser.download.lastDir",r'd:\temp')
profile.set_preference("browser.download.useDownloadDir",True)
profile.set_preference("browser.download.manager.showWhenStarting",False)
profile.set_preference('browser.helperApps.neverAsk.saveToDisk',"application/vnd.ms-excel,Content-Type=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/octet-stream")
profile.set_preference('browser.helperApps.neverAsk.openFile', "application/vnd.ms-excel,Content-Type=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/octet-stream")
browser = webdriver.Firefox(profile)
browser.get("https://docs.ad.sys.com/sites/cloud/Project/Form/FolderCTID=0x01200069047C40C93C3846B74E0776AAD1610A&InitialTabId=Ribbon%2EDocument&VisibilityContext=WSSTabPersistence")
browser.find_element_by_xpath('/html/body/form/div[8]/div/div[3]/div[3]/div[2]/div/div/table/tbody/tr/td/table/tbody/tr/td/div/table[1]/tbody/tr/td/table/tbody/tr[12]/td[4]/div[1]/a').click()
If this is also not working for you, do the following, install the addon tamperdata in firefox and observe the content type for the file you are trying to download and then add that exact text to "browser.helperApps.neverAsk.*" preference. That shall solve your problem!

Categories