changing default download location in chrome using Python selenium - python-2.7

I want to download files into a desired location in chrome using python selenium.however,i am not getting any idea.our framework uses mozwebqa.
how do i change the properties to download files to my desired location.
Just a sample code
#pytest.fixture()
def preparation (self, request, mozwebqa):
openyaml = open("desired txt file")
testdata = yaml.load(openyaml)
openyaml.close()
self.sample = testdata['abc']
def test_sampletest(self, mozwebqa, preparation):
homepage = basePage(mozwebqa).homepage()
homepage.login()
please help, where to set the preference for downloading files to my desired location.

Use below code :-
chromeOptions = webdriver.ChromeOptions()
prefs = {"download.default_directory" : "/some/path"}
chromeOptions.add_experimental_option("prefs",prefs)
chromedriver = "path/to/chromedriver.exe"
driver = webdriver.Chrome(executable_path=chromedriver, chrome_options=chromeOptions)
Source: https://sites.google.com/a/chromium.org/chromedriver/capabilities

Related

How to handle pop up window dialog to download file automatically with firefox profile in python selenium on Linux (Ubuntu) system

I am trying to download the file automatically from system file download dialog by setting preference in firefox profile in my python selenium code , but my code is not working.
Browser : Firefox 72.0
Selenium Version : 3.14
OS : linux Ubuntu
Filetype to download: *.enc (encrypted file type)
Path of firefox in linux : /usr/bin/firefox
Code :
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", "/home/user/Downloads/tests")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-uuencoded,application/octet-stream")
self.driver = webdriver.Firefox(firefox_profile=profile)
Hi #Sum i have resolving, my problem was a different Content-Type.
Use this example to resolve your problem , and to understand your Content-Type: https://stackoverflow.com/a/36356422/12911814
In my case the Content-Type was "application/force-download" not "application/pdf"
profile.set_preference("pdfjs.disabled", True)
profile.set_preference("browser.download.folderList",2)
profile.set_preference("browser.download.manager.useWindow", False)
profile.set_preference("browser.download.dir", "<path>")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf, application/force-download")
These settings worked for me. Hope it might help you.
I have the same problem with Firefox 72.0, but with pdf files. This is the code:
fp = webdriver.FirefoxProfile()
fp.set_preference("pdfjs.disabled", True)
fp.set_preference("browser.download.folderList", 2)
fp.set_preference("browser.download.dir", "/path")
fp.set_preference("browser.download.downloadDir", "/path")
fp.set_preference("browser.download.defaultFolder", "/path")
fp.set_preference("plugin.disable_full_page_plugin_for_types", "application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf, application/vnd.cups-pdf")
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf, application/vnd.cups-pdf")
fp.set_preference("browser.helperApps.neverAsk.openFile", "application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf, application/vnd.cups-pdf")
driver = webdriver.Firefox(firefox_profile=fp)
I have tried all possible preferences, but it always triggers the download alert.
Try this will work like charm ......
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import time
import pyautogui
try :
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.maximize_window()
driver.get("https://www.citysdk.eu/wp-content/uploads/2013/09/DELIVERABLE_WP4_TA_SRS_0.21.pdf")
WebDriverWait(driver, 10).until(lambda d: d.execute_script('return document.readyState') == 'complete')
# Click the OK button and close
time.sleep(5)
webelem = driver.find_element_by_id('download')
webelem.click()
time.sleep(5)
print('press enter')
pyautogui.press('enter')
except Exception as err:
print('ERROR: %sn' % str(err))
driver.quit()
The correct MIME type for .enc is "text/x-uuencoded"
Updated as below in code and it's working :
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/x-uuencoded")

Selenium with Python- Message: 'operadriver' executable needs to be in PATH

Checking whether a website loads in opera using selenium with python, using the code below:
def test_opera_compatability(self):
driver = webdriver.Opera("functional_tests/operadriver")
driver.get("https://www.google.com/")
driver.quit()
It returns the following error:
Message: 'operadriver' executable needs to be in PATH.
Similar code for chrome works as intended, which looks like this:
def test_chrome_compatability(self):
driver = webdriver.Chrome('functional_tests/chromedriver')
driver.get("https://www.google.com/")
driver.quit()
You can use the Key executable_path to pass the absolute path of the operadriver binary as follows:
def test_opera_compatability(self):
driver = webdriver.Opera(executable_path='/path/to/functional_tests/operadriver')
driver.get("https://www.google.com/")
driver.quit()

Scrapy how to save a State between spider runs (via scrapinghub)?

I have a spider that will run on schedule. Spider input is based on Date. From date of last scrape to todays date. So the question is how to save the date of last scrape within the Scrapy project? There is an option to get data from scrapy settings using pkjutil module, but i did not find any reference in the docs on how to write data in that file. Any idea? Maybe an alternative?
P.S. My other option is to use some free remote MySql DB just for this. But looks like more work if simple solution is available.
import pkgutil
class CodeSpider(scrapy.Spider):
name = "code"
allowed_domains = ["google.com.au"]
def start_requests(self):
f = pkgutil.get_data("au_go", "res/state.json")
ids = json.loads(f)
id = ids[0]['state']
yield {'state':id}
ids[0]['state'] = 'New State'
with open('./au_go/res/state.json', 'w') as f:
json.dump(ids, f)
The above solution works fine when ran locally. But I am getting no such file or directory when running the code at Scrapinghub.
File "/tmp/unpacked-eggs/__main__.egg/au_go/spiders/test_state.py", line 33, in parse
with open(savePath, 'w') as f:
IOError: [Errno 2] No such file or directory: './au_go/res/state.json'
The problem is fixed with use of Scrapinghub Colections
And scrapinghub API. Works nice now.
Here is an example code in case somebody will find it usefull.
from scrapinghub import ScrapinghubClient
client = ScrapinghubClient(Your API KEY)
project = client.get_project(Your Project ID)
collections = project.collections
last_accessed = collections.get_store('last_accessed')
last_accessed.set({'_key': 'Date', 'value': '12-54-1235'})
print last_accessed.get('Date')['value']

Python request download a file and save to a specific directory

Hello sorry if this question has been asked before.
But I have tried a lot of methods that provided.
Basically, I want to download the file from a website, which is I will show my coding below. The code works perfectly, but the problem is the file was auto download in our download folder path directory.
My concern is to download the file and save it to a specific folder.
I'm aware we can change our browser setting since this was a server that will remote by different users. So, it will automatically download to their temporarily /users/adam_01/download/ folder.
I want it to save in server disk which is, C://ExcelFile/
Below are my script and some of the data have been changing because it is confidential.
import pandas as pd
import html5lib
import time from bs4
import BeautifulSoup
import requests
import csv
from datetime
import datetime
import urllib.request
import os
with requests.Session() as c:
proxies = {"http": "http://:911"}
url = 'https://......./login.jsp'
USERNAME = 'mwirzonw'
PASSWORD = 'Fiqr123'
c.get(url,verify= False)
csrftoken = ''
login_data = dict(proxies,atl_token = csrftoken, os_username=USERNAME, os_password=PASSWORD, next='/')
c.post(url, data=login_data, headers={"referer" : "https://.....com"})
page = c.get('https://........s...../SearchRequest-96010.csv')
location = 'C:/Users/..../Downloads/'
with open('asdsad906010.csv', 'wb') as output:
output.write(page.content )
print("Done!")
Thank you, be pleased to ask if any confusing information was given.
Regards,
Fiqri
It seems that from your script you are writing the file to asdsad906010.csv. You should be able to change the output directory as follows.
# Set the output directory to your desired location
output_directory = 'C:/ExcelFile/'
# Create a file path by joining the directory name with the desired file name
file_path = os.path.join(output_directory, 'asdsad906010.csv')
# Write the file
with open(file_path, 'wb') as output:
output.write(page.content)

Encoding is not kept

I am using Python 2.7 and using google plus public API to get activity data in a file. I am encountering issues to maintain the json encoding in my file. Double quotes are coming as u'' in my file. Below is my code:
from apiclient import discovery
API_KEY = 'MY API KEY'
service = discovery.build("plus", "v1", developerKey=API_KEY)
activities_resource = service.activities()
request = activities_resource.search(query='India versus South Africa', maxResults=1, orderBy='best',)
while request!= None:
activities_document = request.execute()
if 'items' in activities_document:
with open("output.json", mode='a') as file:
data = str(activities_document['items'])
file.write(data +"\n\n")
request = service.activities().list_next(request, activities_document)
Output:
[{u'kind': u'plus#activity', u'provider': {u'title': u'Google+'}, u'titl.......
I am expecting [{"kind": "plus#activity", .....
I am running my code on windows and I have tried both on DOS and pycharm IDE. I have also run the code on ubuntu machine but same output. Please let me know what I am doing wrong.
The json module is used for generating JSON. Use it.