Firefox webdriver-Changing the default download location at runtime - selenium

How do I set a default download location for Firefox at runtime. Is there any way to prevent the folder selection popup from coming?

I would follow the code found on this blog
Something like:
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList",2)
profile.set_preference("browser.download.manager.showWhenStarting",False)
profile.set_preference("browser.download.dir",getcwd())
profile.set_preference("browser.helperApps.neverAsk.saveToDisk","text/csv")
browser = webdriver.Firefox(firefox_profile=profile)
Hope that helps.

Related

How do I set Chrome upload directory with Selenium & VBA

How do i set default upload path with selinium and chrome. eg going to a webpage, and upload. this opens a fileexploere, but it always opens c:\users[me]
I want to be able to set anoter default path.
i have seen this to set download path
driver.SetPreference "download.default_directory", "c:\temp"
and i tried to alter it
driver.SetPreference "upload.default_directory", "c:\temp"
but it dosnt work.
Chrome Version 102.0.5005.115 - win10
Found it
from this helpfull link
Where can I find a list of all available ChromeOption arguments?
theres a link to "List of preferences:"
in here i found
selectfile.last_directory
So i set
driver.SetPreference "selectfile.last_directory", "c:\temp" or what you want

Is there a way that you can use the browser profiles on your desktop as the Selenium webdriver? [duplicate]

So whenever I try to use my Chrome settings (the settings I use in the default browser) by adding
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\Users\... (my webdriver path)")
driver = webdriver.Chrome(executable_path="myPath", options=options)
it shows me the error code
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes n 16-17: truncated \UXXXXXXXX escape
in my bash. I don't know what that means and I'd be happy for any kind of help I can get. Thanks in advance!
The accepted answer is wrong. This is the official and correct way to do it:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument(r"--user-data-dir=C:\path\to\chrome\user\data") #e.g. C:\Users\You\AppData\Local\Google\Chrome\User Data
options.add_argument(r'--profile-directory=YourProfileDir') #e.g. Profile 3
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
driver.get("https://www.google.co.in")
To find the profile folder on Windows right-click the desktop shortcut of the Chrome profile you want to use and go to properties -> shortcut and you will find it in the "target" text box.
To get the path, follow the steps below.
In the search bar type the following and press enter
This will then show all the metadata. There find the path to the profile
As per your question and your code trials if you want to open a Chrome Browsing Session here are the following options:
To use the default Chrome Profile:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\AtechM_03\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
driver.get("https://www.google.co.in")
Note: Your default chrome profile would contain a lot of bookmarks, extensions, theme, cookies etc. Selenium may fail to load it. So as per the best practices create a new chrome profile for your #Test and store/save/configure within the profile the required data.
To use the customized Chrome Profile:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("user-data-dir=C:\\Users\\AtechM_03\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 2")
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
driver.get("https://www.google.co.in")
Here you will find a detailed discussion on How to open a Chrome Profile through Python
This is how I managed to use EXISTING CHROME PROFILE in php selenium webdriver.
Profile 6 is NOT my default profile. I dont know how to run default profile. It is IMPORTANT not to add -- before chrome option arguments! All other variants of options didnt work!
<?php
//...
$chromeOptions = new ChromeOptions();
$chromeOptions->addArguments([
'user-data-dir=C:/Users/MyUser/AppData/Local/Google/Chrome/User Data',
'profile-directory=Profile 6'
]);
$host = 'http://localhost:4444/wd/hub'; // this is the default
$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY, $chromeOptions);
$driver = RemoteWebDriver::create($host, $capabilities, 100000, 100000);
To get name of your chrome profile, go to chrome://settings/manageProfile, click on profile icon, click "Show profile shortcut on my desktop". After that right click on desktop profile icon and go to properties, here you will see something like "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --profile-directory="Profile 6".
Also I recommend you to close all chrome instances before running this code. Also maybe you need to TURN OFF chrome settings > advanced > system > "Continue running background apps when Google Chrome is closed".
None of the given answers were working for me so I researched a bit and now the working code is for is this one. I copied the user dir folder from Profile Path from chrome://version/ and made another argument for the profile as shown below:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument('user-data-dir=C:\\Users\\gupta\\AppData\\Local\\Google\\Chrome\\User Data')
options.add_argument('profile-directory=Profile 1')
driver = webdriver.Chrome(executable_path=r'C:\Program Files (x86)\chromedriver.exe', options=options)
driver.get('https://google.com')
Are you sure you are meant to be putting in the webdriver path in the user-data-dir argument? That's usually where you put your chrome profile e.g. "C:\Users\yourusername\AppData\Local\Google\Chrome\User Data\Profile 1\". Also you will need to use either double backslashes or forward slashes in your directory path (both work). You can test if your path works by using the os library
e.g.
import os
os.list("C:\\Users\\yourusername\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 1")
will give you the directory listing.
I might also add that occasionally if you manage to crash chrome while running webdriver with a nominated user profile, that it seems to record the crash in the profile and the next time you open chrome, you get the Chrome prompt to restore pages after it exited abnormally. For me personally this had been a bit of headache to deal with and I no longer use a user profile with chromedriver because of it. I could not find a way around it. Other people have reported it here, but none of their solutions seemed to work for me, or were not suitable for my test cases. https://superuser.com/questions/237608/how-to-hide-chrome-warning-after-crash
If you don't nominate a user profile it seems to create a new (blank) temporary one each time it runs
Make sure you've got the path to the profile right, and that you double escape backslashes in said path.
For example, typically the default profile on windows is located at:
"C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data\\Default"
I managed to launch my chrome profile using these arguments:
ChromeOptions options = new ChromeOptions();
options.addArguments("--user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data");
options.addArguments("--profile-directory=Profile 2");
WebDriver driver = new ChromeDriver(options);
You can find out more about the web driver here
You simply have to replace the '\' to '/' in your paths and that'll resolve it.
Get profile name by navigating to chrome://version from your chrome browser (You'll see Profile Path, but you only want the profile name from it (e.g. Profile 1)
Close out all Chrome sessions using the profile you want to use. (or else you will get the following error: InvalidArgumentException)
Now make sure you have the code below (Make sure you replace UserFolder with the name of your userfolder.
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\EnterYourUserFolder\\AppData\\Local\\Google\\Chrome\\User Data") #leave out the profile
options.add_argument("profile-directory=Profile 1") #enter profile here
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe", chrome_options=options)
this worked for me 100% and it showed up my selected profile.
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# path of your chrome webdriver
dir_path = os.getcwd()
user_profile_path = os.environ[ 'USERPROFILE' ]
#if "frtkpr" which is ll be your custom profile does not exist it will be created.
option.add_argument( "user-data-dir=" + user_profile_path + "/AppData/Local/Google/Chrome/User Data/frtkpr" )
driver = webdriver.Chrome( dir_path + "/chromedriver.exe",chrome_options=option )
baseUrl = "https://www.facebook.com/"
driver.maximize_window()
driver.get( baseUrl )

Loading Edge 83 with Selenium, in EdgeHTML mode (UseChromium = false). Returns Error

I'm trying to launch Edge using the EdgeHTML renderer mode, within Selenium, using Visual Studio C#.. I'm using the version 83.0.478.58 (64bit) and the driver 83.0.478.58 (64bit). Installed via nuget.
If I launch the browser using it works fine...
EdgeOptions options = new EdgeOptions();
options.UseChromium = true;
options.AddUserProfilePreference("download.default_directory", #"C:\temp\");
EdgeDriver edgeDriver = new EdgeDriver(options);
By default UseChromium is False, which should launch the browser in the EdgeHTML renderer. If I use the below code it will error out.
EdgeOptions options = new EdgeOptions();
options.AddUserProfilePreference("download.default_directory", #"C:\temp\");
EdgeDriver edgeDriver = new EdgeDriver(options);
The browser will open a new instance, but once the browser is open. The code bugs out and returns the error message,
OpenQA.Selenium.WebDriverException: 'The new session command returned a value ('Unknown error') that is not a valid JSON object.'
If I just try and simply initialise without the options, I still get the same error.
Anyone got any ideas? Looking around I see documentation and guides all over that launch the browser without UseChromium.
Cheers
Looks like you are trying to set the default download directory for the MS Edge legacy (EdgeHTML) browser using the Selenium code.
I want to inform you that it is possible to set the default download directory for the MS Edge (Chromium) browser as you had already tried that successfully but it is not possible to set the default download directory for the MS Edge legacy (EdgeHTML) browser using the Selenium code.
As a workaround, you can try to set the download directory manually.
References:
Change the default file download path for Edge using selenium
Change default download location in Edge

Cannot Download PDF's Using Selenium/Python 3.x, But It works When I do it Manually

I read through a ton of answers to this query of mine but couldn't find anything specific. Hence asking here
Here's the scenario, On a webpage, when I click a download button, it downloads a PDF file correctly, On the browser, I have set the Firefox preferences to save the file rather than open in preview.
However, when I run my selenium/Python script, the download keeps opening in the preview, there are other PDF downloads on the page and they work fine. Upon inspecting both the download buttons, the only difference I see is the one that does not download has a relative URL in its href value.
I am also using the following firefox options settings in my script, but with no help. Please guide me in the right direction. Thanks in advance!
**************************
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList", 2)
fp.set_preference("browser.download.manager.showWhenStarting", False)
fp.set_preference("browser.download.dir", 'Path to Save The file')
fp.set_preference("pdfjs.enabledCache.state", False)
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf")
fp.set_preference("pdfjs.disabled", "true")
# disable Adobe Acrobat PDF preview plugin
fp.set_preference("plugin.scan.plid.all", "false")
fp.set_preference("plugin.scan.Acrobat", "99.0")
self.driver = webdriver.Firefox(firefox_profile=fp,executable_path="path to my geckodriver")
self.driver.get("url")
I had the same problem - setting to disable pdfjs only worked when clicked manually on about:config page. Turned out that what seems to have solved the issue was (Firefox 60.6.1ESR):
profile.setPreference("pdfjs.disabled", true);
profile.setPreference("pdfjs.enabledCache.state", false); // <= THIS

How to deal with Java warning popup?

While running WebDriver automation scripts I came across a situation where it is trying to open a page which contain one segment with live camera (Made with Java applet). Once script reaches to this page - a Security Warning alert (with allow and not allow) shows up and blocks the execution process. Is this something that anyone faced before - actually I am looking for an option to block this security warning to get displayed on the page.
A popup is coming where i want to click on the "Allow". How to move the focus to the new popup window and click on Allow.
Can anyone please help me for the above problem?
I was having problems accepting the java applet "Allow"
My solution was to create a firefox profile that had the settings to always activate the plugin:
FirefoxProfile fp = new FirefoxProfile();
fp.setAcceptUntrustedCertificates( true );
fp.setPreference( "security.enable_java", true );
fp.setPreference( "plugin.state.java", 2 );
WebDriver d = new FirefoxDriver( fp );
Where plugin.state.java:
plugin.state.java = 0 --> never activate
plugin.state.java = 1 --> ask to activate
plugin.state.java = 2 --> always activate
This might get you closer...
Selenium uses a different firefox profile because Java was inactive for me and I did not have my firebug plugin in the Firefox browser Selenium launched. I would have to open another Firefox to use Firebug.
I found my default Firefox profile by searching %appdata% in the start menu then clicking on Roaming/ Mozilla/ Firefox/ Profile/ and then it gave my default profile name.
You can also open the firefox help menu (? logo) & click troubleshoot info... click Show Profile Folder
I then configured selenium to use my default profile so Java was enabled and Firebug was available in the browser Selenium launched:
Make sure that you use "/" in selenium even though it may use "\" in the windows path location
fp = webdriver.FirefoxProfile('C:/Users/xxx/AppData/Roaming/Mozilla/Firefox/Profiles/41s7nq9o.default')
driver = webdriver.Firefox(fp)
driver.get('www.stackoverflow.com')
where 41s7nq9o.default is the name of your default profile