How can I send a file to the browser in a headless selenium session (as opposed to clicking on an "upload" button)?
For example, how do I add a file to a form like this without using the gui:
In most cases there is an input element with type file there, so you can directly send your file path to it.
For example if your file path on the disk is C:\\Users\\Desktop\\test.png your code will be:
file_path = "C:\\Users\\Desktop\\test.png"
upload_element = driver.find_element_by_xpath("//input[#type='file']")
upload_element.send_keys(file_path)
Related
Iam trying to open ftp url in file explorer.
url = QUrl("ftp://192.168.1.127:15010/intrusionfiles/detectionfiles/",QUrl.TolerantMode)
url.setScheme("ftp")
QDesktopServices.openUrl(url)
When I try it this way it asks how I want it to open. How do I get it to open directly in file explorer?
Solved my problem by starting explorer.exe using qprocess.
p = QProcess()
p.startDetached("C:\\Windows\\explorer.exe", ["ftp://admin:admin#192.168.1.127:15010/intrusionfiles/detectionfiles/21-09-21/"])
I currently use Selenium to click a download link to download a CSV file into directory, wait for the download to complete, then read the file from directory into a python variable.
I want to deploy the script into a Docker container and as far as i'm aware, I can't load the CSV the way I currently am.
I also don't know how the CSV file is created by the download button so I can't call webdriver.Firefox.execute_script().
Is there a way to intercept Firefox when downloading to see the file and save it straight to a variable at that point?
Is there an easy way to see how the CSV is created? (as I can't read the website code)
If no to the above, is there a way I can perform my current actions inside a Docker container that will be hosted on a cloud server?
My current code for reference
# Driver set-up
firefox_driver_path = "Path to geckodriver.exe"
downloaded_csv_path = "Path to specific .csv file when downloaded"
firefox_options = webdriver.FirefoxOptions()
firefox_options.headless = True
firefox_options.set_preference("browser.download.dir", "Path to download .csv file")
firefox_options.set_preference("browser.download.folderList", 2)
firefox_options.set_preference("browser.download.useDownloadDir", True)
firefox_options.set_preference("browser.download.viewableInternally.enabledTypes", "")
firefox_options.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf;text/plain;application/text;text/xml;text/csv;application/xml")
driver = webdriver.Firefox(executable_path=firefox_driver_path, options=firefox_options)
wait = WebDriverWait(driver, 10)
# Navigate to webpage, perform actions
driver.get("website URL")
# Perform other actions to navigate to the download button
driver.find_element_by_id('DownloadButton').click()
# Check if download is complete
time_to_wait = 10
time_counter = 0
while not os.path.exists(downloaded_csv_path):
time.sleep(1)
time_counter += 1
if time_counter > time_to_wait:
sys.exit("First CSV download didn't complete")
# Read the CSV data and delete the downloaded file
with open(downloaded_csv_path) as csvFile:
my_CSV = csv.reader(csvFile, delimiter=',')
os.unlink(downloaded_csv_path)
UPDATE
One download button has what seems an Angular function attached to it ng-click="$ctrl.onExportTransactions()".
I know nothing of angular and after searching every source file for the website (including 3 that are named "Angular xxx", I can't find function called "onExportTransactions". Is there a way this function can be invoked from Python/Selenium?
I have a web application written in Python and Django framework that allows uploading a file. The uploaded file path is shown in a textbox. When I browse and select a file it is shown as c:\fakepath\Sample.docx in the textbox and when I click the upload button the file upload happens hassle-free.
But if I provide the absolute path in the textbox as C:\xxx\xxx\xxx\sample.docx and click the upload button, the file doesn't upload.
Html code to create the upload in a textbox:
<input id="txt" type = "text" value = "Choose File" size="40" align="center"
onclick ="javascript:document.getElementById('file').click();">
<input id = "file" type="file" style='visibility: hidden;' name="file1"
onchange="ChangeText(this, 'txt'); Filevalidation()">
Python code when I try to extract the file uploaded
file = request.FILES['file1']
Error while giving the absolute path:
raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'file1'
That happens because you're not sending any actual file in the request by just typing the file path, so, that 'file1' key is not filled in the request.FILE dictionary. So, you can do:
file = request.FILES.get('file1', '')
This will handle the case when that key does not exist and give a default value (if not passed, default would be None).
But still, doing this just avoid getting the error, but the file won't be sent anyways. You can't send a file to the server by just writing the path where it is stored.
In Flask I want the user to be able to download a file from a S3 bucket using Boto. Of course if Flask downloads something from S3 it stores the file on the server. However I want the the file to be downloaded to the users machine if they click a download button. Is that possible? My idea was the following. When the download button gets clicked Flask fetches the file from S3 and stores it on the server afterwards the users machine downloads the file. Afterwards the file on the server gets deleted. Please tell me if there is a better way. If I do it like this it works. Unfortunately I need to render my dashboard again after the file was downloaded so I can't return the file.
Flask:
#app.route('/download')
#login_required
def dowloadfile():
try:
#Boto3 downloading the file file.csv here
return send_file('file.csv', attachment_filename='file.csv')
except Exception as ermsg:
print(ermsg)
return render_template('dashboard.html', name=current_user.username, ermsg=ermsg)
HTML:
<button class="buttonstyle" onclick="showImage();">Download</button>
The problem is that when the download button is clicked a full screen image is shown which is my loading screen. This loading screen disappears when the function is done and a new html is rendered. With the code above the loading screen appears and stays forever even when the file is aready downloaded to the users machine. How could you fix that?
I want to make a python script that opens a text file that the user can update before continuing (for configurations) and then continue the script once the text editor closes. Is there something in python that will allow me to do this?
on windows using notepad:
import os
cf = "config.txt" # or full path if you like
if not os.path.exists(cf):
f = open(cf,"w"); f.close() # create to avoid notepad message
os.system("notepad "+cf) # call waits until user closes the file
# now the file has been edited by the user