Python and selenium: Force no download window [duplicate] - python

This question already has answers here:
Python: Unable to download with selenium in webpage
(5 answers)
Closed 4 years ago.
Using geckodriver and Firefox v63, I try to download a CSV file from a website without causing the download prompt to appear.
My code is:
from selenium import webdriver
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.dir', '/Path/to/download')
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/csv')
browser = webdriver.Firefox(profile, options=options)
browser.get('http://www.website.com')
download = browser.find_element_by_css_selector('selector')
download.click()
It does not work and I don't understand why. I don't get an error and the code executes, but it shows the Firefox download window and only safes the file if I click OK. I'd like to avoid clicking manually because I want to automate the task.

Open up FireFox and try this
Goto Preferences -> General
and then pick the folder you want to download to and uncheck the "always ask"
Give this a shot, good luck!

Related

Selenium won't open specific chrome profile python [duplicate]

This question already has answers here:
How to open a Chrome Profile through Python
(2 answers)
How to use Chrome Profile in Selenium Webdriver Python 3
(12 answers)
How to open the Default Chrome Profile through Selenium, ChromeDriver and GoogleChrome
(4 answers)
Closed 2 years ago.
I have a chrome profile (called Profile 1) which I would like to control with Selenium. Whenever I try to open that chrome profile, Chrome opens without my profile, like it was my very first time opening Chrome.
My code is the following:
options = Options()
options.add_argument("user-data-dir=C:\\Users\\Ákos\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 1")
driver = webdriver.Chrome(executable_path="C:\\pythonScriptjeim\\chromedriver.exe", options=options,)
driver.get("https://www.google.com/")
Funny thing is if i delete Profile 1 from the end like this, Chrome opens with my default profile:
options.add_argument("user-data-dir=C:\\Users\\Ákos\\AppData\\Local\\Google\\Chrome\\User Data")
So my problem is, that anything written after User Data doesn't seem to have any effects on chrome.
Any suggestions on how I could open the profile i'd like to control?

How to use Python Selenium on existing browser? [duplicate]

This question already has answers here:
How can I reconnect to the browser opened by webdriver with selenium?
(4 answers)
Closed 2 years ago.
I'm new to Selenieum and still trying to understand how the different pieces work together. I am trying to automate some interaction tasks on an internal website using Python. However, I am trying to use the existing Chrome browser session AFTER I've authenticated with my username and password rather than launching a new browser. I'd like to start where my browser is currently and I'd also like to use a separate profile when launching chrome as to not pollute the default Chrome browser (not sure this is necessary). The driver is currently on a folder on my desktop and I also created a variable ChromeDriver location in my PATH environment variable on my Mac but I am not sure that is really necessary. I also created I've researched several similar post and found the code below on SO, but when I run it still opens a new browser and drops me at the login screen.
from selenium import webdriver
PATH = 'where chrome driver is located'
driver = webdriver.Chrome(PATH)
url = driver.command_executor._url
session_id = driver.session_id
driver = webdriver.Remote(command_executor=url,desired_capabilities={})
driver.close()
driver.session_id = session_id
driver.get("https://whereidliketobeafterlogin")
Based on this post here it looks like your answer may already be available.
I had a similar requirement but it was part of a larger application so i created a quick dialog box using the built in tkinter library to take a username and password so that Selenium could do the log in for me, that way my user data was never stored anywhere.
As far as using a separate profile than whats currently logged in your browser and all, when Selenium web driver starts up a new browser it functions as if it is in the incognito/private version, not retaining any of the login/session info unless specified through parameters.
Lastly, the PATH environment variable isnt entirely necessary, as long as you give filepath to the driver on your desktop when you create your webdriver object you are good.

How to get rid of getting alert "This type of files may harm your computer. Do you want to keep or discard?" [duplicate]

This question already has an answer here:
How to download XML files avoiding the popup This type of file may harm your computer through ChromeDriver and Chrome using Selenium in Python
(1 answer)
Closed 3 years ago.
I am trying to download files from website using selenium and chrome webdriver in python and i am getting the alert to keep or discard. I have tried every way as mentioned on stackoverflow to change the chrome option and preferences, but of no use.
Thanks in Advance
Have you tried this?
chromeOptions = webdriver.ChromeOptions()
prefs = {'safebrowsing.enabled': 'false'}
chromeOptions.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chrome_options=chromeOptions)

Can't turn off images in Selenium / Firefox

I am looking to disable images in Firefox when using Selenium. It should be a simple update of the preferences in firefox, which is documented on the instructions on Disable images in Selenium Python
However when i run, images display, and when i enter about:config, the value for permissions.default.image is still 1, rather than 2 which i have tried setting it to.
My code (written in Python) is:
from selenium import webdriver
firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference("permissions.default.image", 2)
driver = webdriver.Firefox(firefox_profile)
driver.get(web_address)
For reference, this code works perfect with another change to preference e.g. turning off csv files with the line firefox_profile.set_preference("permissions.default.stylesheet",2). The only difference i can tell between the csv setting and the image one, is that the line permissions.default.image already exists in about:config (i.e. without me setting it), however the line permissions.default.stylesheet does not. ... it seems that i can add new lines in with the value i want, but not change an existing one (or it is beein over-ridden by Selenium after i enter my value).
From what I understand, this problem is related to the following Firefox issues:
Remove "Load images automatically" checkbox from Prefs
Get rid of options that kill our product
That means that permissions.default.image is frozen, cannot be changed and does nothing.
Alternatives:
use Image Block extension
switch to Chrome (Disable images in Selenium ChromeDriver)
I had this problem. the solution is bellow in 3 steps.
1- creating new profile for Firefox. in Windows completely close Firefox. press (Window+R) , write firefox.exe -p then press enter and create a new profile.
2-open Firefox with the created profile then open about:config in navigation bar and find permissions.default.image and make it's number 2.
3-change your code like bellow
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("your_profile_name");
WebDriver driver = new FirefoxDriver(myprofile);
I've created a code sample for disabling images.
#DISABLE IMAGES ON FIREFOX
def disable_images(driver):
driver.get("about:config")
driver.find_element("id","warningButton").click()
searchArea=driver.find_element("id","about-config-search")
searchArea.send_keys("permissions.default.image")
editButton=driver.find_element("xpath","/html/body/table/tr[1]/td[2]/button")
editButton.click()
editArea=driver.find_element("xpath","/html/body/table/tr[1]/td[1]/form/input")
editArea.send_keys("2")
saveButton=driver.find_element("xpath","/html/body/table/tr[1]/td[2]/button")
saveButton.click()

Python Selenium Custom FireFox Profile Not Saving Data [duplicate]

This question already has answers here:
Python / Selenium / Firefox: Can't start firefox with specified profile path
(2 answers)
Closed 7 years ago.
So I've been experimenting with a custom firefox profile in my selenium project so that websites don't think I'm a new user every time. This is the code I'm using:
profile FirefoxProfile('C:\\Users\\Scott\\AppData\\Local\\Mozilla\\Firefox\\Profiles\\t3bxt79j.bot_profile')
binary = FirefoxBinary('C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe')
browser = webdriver.Firefox(firefox_profile=profile, firefox_binary=binary)
This code should link up to my profile that I created and the binary (which is already tested and working). However, every time I go to the website it acts like I'm a new user. None of my bookmarks on this profile are saving either and I'm making sure that I load firefox with the new profile.
It does save, but does not get back to you.
I've answered that just now here: https://stackoverflow.com/a/33350778/2480481.
Profile is made to allow you run test with certain personalization of the firefox, settings, extensions, etc, not for save it.
But yes, I miss too a parameter to make it "fixed" instead of temporal.

Categories