Download file from sharepoint using selenium webdriver python - 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!

Related

Profile preferences doesn't apply in Selenium Python

Here is my code. I try to download file using selenium so I want to disable pop up save window but all I've tried didn't work for me. Save window keep showing up. I use Mozilla Firefox and Windows 10.
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", "./")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "attachment/csv")
binary = '/Users/User/Documents/Geckodriver/geckodriver.exe'
browser = webdriver.Firefox(executable_path = binary, firefox_profile = profile)
If it is important I use Firefox as my regular browser maybe it can somehow affect geckodriver.
I add update line before binary line and it didn't help me.
profile.update_preferences()
I found all these preferences in about:config and they all actually have been applied:
example
Actually I try to download mp3 file if this matters
Once you set all the preferences through profile.set_preference() lines, finally you need to update the preferences using the line:
profile.update_preferences()
So effectively, your code block will be:
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "attachment/csv")
profile.update_preferences()
binary = '/Users/User/Documents/Geckodriver/geckodriver.exe'
browser = webdriver.Firefox(executable_path = binary, firefox_profile = profile)

Python Firefox Selenium Load Profile

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)

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)

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

Setting up Firefox profile for WebDriver in Python not to ask popups on download

I know there are a bunch of other questions on this topic, but I can't seem to figure out why I can't set up a custom profile in Firefox when using selenium in Python 2.7.9.
Here's my code:
import requests
from selenium import webdriver
content_type = requests.head('url').headers['content-type']
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","/users/me/downloads")
fp.set_preference("browser.helperApps.neverAsk.openFile",content_type)
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", content_type)
browser = webdriver.Firefox(firefox_profile = fp)
When I run my code to perform an automatic download I still get a popup box. The files I'm trying to download are csv's but the content type I get from running the code above is "text/plain". I've also tried "text/csv" and "text/csv,application/vnd.ms-excel". Nothing has worked. I'm running Firefox 39.0.

Categories