How can I open an html file with code in PyCharm - python

I am developing a plugin for PyCharm in Java.I use Pyan3 to generate a static graph. Pyan3 gives me an HTML file as a result, which I want to display.
I tried with the webbrowser but failed.
import webbrowser
import os
webbrowser.open('file://' + os.path.realpath('myuses2.html'))
Is it possible to open the html file inside pyCharm? (from code line)
(If it is possible to open, than there is any way to give back information from the HTML? I want the code to jump to a function name when the user clicks on it.)

Related

What code can I use to download a csv file that requires some steps after logging in to the website?

After some research on my problem, it seems I should use either requests or urllib or both.
So basically, I am trying to learn the code I need to download a csv file from this url:
https://globalaccess.sustainalytics.com/#/tools/0
The way I manually download my files is as follows: first, I need to log in using username and password. Next I have to go to a tab called "Screening" that takes to me another page that has several buttons called "Generate". I click a specific generate button (it's always the same one) among the option to get the excel file. After that I have the option to save the file or open from a little window within the website.
My question is what code can I use on Python to download and save the file in a particular folder?
Use Selenium
https://selenium-python.readthedocs.io/
You'll need to download a 'chromedriver' to the same directory as your python script, then use the intro tutorial on the selenium docs site to drive the browser to type/click where you want.
If you use chrome you can right click on any given link/input box click inspect, then in the window that comes up right click the bit of highlighted code and 'copy xpath'. Use the find element by xpath function in Selenium to send keys or clicks to that element.

Using Python, upload image on https://cloud.google.com/vision/ and read JSON script

I want to write a python script to automate uploading of image on https://cloud.google.com/vision/ and collect information from JSON tab there. I need to know how to do it.
Till now, I'm only able to open the website on chrome using following code:-
import webbrowser
url = 'https://cloud.google.com/vision/'
webbrowser.open_new_tab(url + 'doc/')
I tried using urllib2 but couldn't get anything.
Help me out please
you have to use google-cloud-vision lib
there is a sample code in this docs
https://cloud.google.com/vision/docs/reference/libraries#client-libraries-install-python
you can start from here

Python Selenium-webdriver close Open file windows pop-up

I'm using python and selenium chrome driver to click on an upload file button which opens an open file window as shown:
My code uploads the files without interacting with the window. What I'm trying to do is close this window but am not sure how to go about doing this.
I've read various other posts with the similar problem but none I could find really gave me what I was looking for. I understand selenium can't access the window, and I've read I need a different module to interact with it. What would be the best way to do this?
In the end I was able to upload the item directly into the page by finding the upload button and sending it the file path.
driver.find_element_by_xpath("//*[#id='upl-fileInp']").send_keys("C:/Users/user/folder/file.jpg")
Workaround
You need put inside element of upload the root file. Don't open the upload by button, just insert /root/of/file/file.pdf inside input upload value using Javascript. Remember, that's my workaround... haha

Python - Retrieve file for a browser page's <input type="file"> through a filepath (or any alternative)

I'm trying to automate uploading a file into a browser page through Python in Terminal. The browser page's code has
<input type="file" id="upload_input"/>
which opens an OS level dialog box, where I'm getting stuck. Is there a way to somehow override the OS level dialog box that opens by pointing it to a filepath or any alternative using Python? I'm overriding because I've been using Selenium to interact with the browser page and my code gets stuck here, where I have to interact with an OS level dialog box.
Note: the page isn't my page so I can't go in and change the html code.
Thanks!
Try this:
driver.find_element_by_id("upload_input").send_keys(r"C:\path_to_file.txt")
driver.find_element_by_id("submit_button").click()
If the test file is located in the same folder as your test script you can use the following:
driver.find_element_by_id('upload_input').send_keys(os.getcwd()+"/testupload.xlsx")
driver.find_element_by_css_selector('input[type="submit"]').click()
In this case, os.getcwd() locates the 'current working directory' which is where the test script is located. I add the filename with string concatenation, you can use the same method to add for instance a directory. Unlike explicit file paths (such as C:\temp\myfile.txt), this way will work regardless of your OS as long as the relative file structure is the same across the machines.

Need to download a file from a web browser using python [duplicate]

This question already has answers here:
Python download a file
(2 answers)
Closed 9 years ago.
I am trying to use Python 2.7 to open a web browser and download and save a linked kml file.
The website is:
rmgsc.cr.usgs.gov/outgoing/GeoMAC/ActiveFirePerimeters.kml
When I run the code below, a new kml file appears to be saved on my drive, however it does not bring over the entire file. Instead of 154MB, it brings over a file that is only 17 bytes.
I can either go to the website (rmgsc.cr.usgs.gov/outgoing/GeoMAC/) and then click on a link named "ActiveFirePerimeters.kml", or go to the website with the kml file name included which automatically initiates a download. In IE9 I would then need to click a "Save" button, however in Chrome it appears to automatically download. However in both only 17bytes come across....
Here is my code for Internet Explorer:
import os, webbrowser
target_dir = r"C:\DataTest"
filename = "ActiveFirePerimeters.kml"
fullname = os.path.join(target_dir,filename)
with open(fullname,"w") as f:
f.write("<html>....</html>")
url = "rmgsc.cr.usgs.gov/outgoing/GeoMAC/ActiveFirePerimeters.kml"
webbrowser.open(url,True,True)
os.system("taskkill /F /IM iexplore.exe")
Are you required to use Internet Explorer for some reason to accomplish this task ? I would suggest taking a look at the urllib module : http://docs.python.org/library/urllib.html. In particular, you can download a resource easily using :
import urllib
urllib.urlretrieve(source_url, local_file_path)

Categories