Importing a file to a web page [SELENIUM] - html

I am creating a selenium script to automatically log in to a webpage, and then import a .xls file. However I am stuck in the part when the webpage prompts me to select a file from my computer. How do I code to send the keys with the file path and press "enter"? Thanks in advance!

To upload a file with Selenium you can send the file's path to input element with type file on the web page.
If, for example your file path is C:\your_file.xls your code will be something like this:
file_path = 'C:\your_file.xls'
upload_input = driver.find_element_by_xpath('//input[#type="file"]')
upload_input.send_keys(file_path)

Related

Adding an excel command to open a csv file to the start of a relative path in HTML

I'm currently using the pytest-html plugin to generate a test report in HTML. These HTML test reports will be put into a folder with related .csv files, and uploaded onto SharePoint for others to download and see. pytest-html allows me to add more HTML to the report generation, and I want to include a local file path link that automatically opens the excel file.
To open an excel file for editing through an HTML file path link, I add ms-excel:ofe|u| to the beginning. For example:
'Open in Excel'
The command is perfectly fine with an absolute path, but what if I need to put it at the start of a relative path? For example:
'<a href="ms-excel:ofe|u|./aaa.csv'
In this case, excel tries to open a file literally named ./aaa.csv and doesn't properly go into the relative path where the HTML file is located.
How can I directly open an excel file through a relative HTML file path link?

Saving web page as an html file on computer

I want to save the source code for my website page into my computer. I know that I have to use an http request to download the source code for my web page into the computer as a html file. I want to run a diff to track changes between two html files for a web page. I am wondering how to implement a program to perform the function of saving a web page as an html file on my computer. Please help it is really appreciated. I want to solve the problem programatically. I was researching on this topic and found that httpget, and selenium scripts can achieve this task but I am struggling with the implementation.
With linux you can just use wget.
wget http://google.com
that will save a file called index.html on your computer.
Programmatically you can use python:
import urllib2
# create a list of urls that you want to download
urls = ['http://example.com', 'http://google.com']
# loop over the urls
for url in urls:
# make the request
request = urllib2.urlopen(url)
# make the filename valid (you can change this to suit your needs)
filename = url.replace('http://', '')
filename = filename.replace('/','-')
# write it to a file.
with open(filename + '.html', 'w') as f:
f.write(request.read())

html file download on node server

Within my public folder on my node.js server I have a standard html website set up.
Im trying to download files from a webpage using
Download
but i keep getting:
my folder structure looks like this(shortened)
Files
-Solar.zip
public
-project.html
Where project.html is where I have the button to download.Should i be using a get instead? or is it not html related at all?
You need to add full path to zip file in href attribute:
Download
download attribute just allows you to set a separate file download name than the actual link. For example:
Download
will start downloading file a.zip but will rename it to b.zip

how to write to output a command from a html file to an excel file?

I have a html file which I want to write into an excel file. I thought the command I should put in my .bat file would be myfile.html > myfile.xls OR myfile.html >> myfile.xls
What it is doing is creating the file but when I open it is renamed correctly but blank in terms of content. the html file is just a massive table of data so surely it can just post it to the excel file correctly?
if the html page is in IE you can create a button at the end of table which on click executes a javascript and download table content in .xls using activeXobject or you can redirect to another jsp page where response type is set for excle.

how to get browsed file location by user when he wants to download a file

this is my html code to make user to download a file and it is hitting controller
window.location.href="#routes.ListManagementController.downloadList("+listName+")?listname="+listName;
this is my controller code:
String listName = Form.form().bindFromRequest().get("listname");
response().setContentType("application/x-download");
response().setHeader("Content-disposition", "attachment;filename="+listName+"_data_export.csv");
The above two respose() statements make a pop up to download a file I want the browsed file location
File file = new File("C:/csv/" + filename);
So, using servlet api we can write the content into browsed file location using respose.getOutputStream() method. In play there are is no support for servlet. I want browsed file location selected by user so that i can give that location to File and write the file over there.
You can't get the location of a directory on the client, and even if you could, your server side could wouldn't be able to write to it (since it would usually be on a different computer).