How to open links in a python console application - python

I'm trying to get myself a decent RSS reader in python. It's supposed to be a pure console application.
My question is-as mentioned in the title-how to open a link within this console. So e.g theres a recent feed entry and i want to click on it and it should open the linked website in a browser.
Is this possible and if yes I would be glad if you could help me

Since you're using Windows, you can use Windows console functions to detect clicks on the console, translate the click positions (which will already be character positions) to links via hit-testing, and hook them up to e.g. webbrowser.open to open a link.
Input handling for console applications is described here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682079%28v=vs.85%29.aspx.

Related

Create a shortcut using URL with python for windows 10

I have multiple links scraped, and I want a code that creates a shortcut for each link and saves on the desktop. So whenever we click that link, It opens that particular URL on chrome. Can we do that using python ?
Take a look at the answer to this question. It solves both aspects of your problem:
finding the correct desktop folder using the winshell package
creating the link-file (*.url) by writing a text file with the following content
[InternetShortcut]
URL=https://stackoverflow.com/
Note that on my machine it asked whether to open the *.url file with my default internet browser or some other program when I double-clicked it.

Using python - pywinauto not able to get control of a tab

How can I get control of a tab using pywinauto? I can easily see the "Compras/Vendas" using print_control_identifiers(), but when I try to click it I'm receiving the error below
The error:
The tab I'm trying to select:
Edit:
I learned a few things about TabControl reading the documentation, it works very well in some apps, but not in the one I'm trying to automate. My code now:
my code working
my code not working

Python. How to print text to console as hyperlink?

I'm working on a console application. My application uses urwid lib. In some cases, I need to show very long hyperlinks as short text inside table columns. I want to open links in the browser when I click on the text inside the column.
So, my question is:
It is possible to print text as a hyperlink to the console?
Can you provide a small example of how to print text as a hyperlink using python?
A bit late, but there totally is a way to do that now, without curses or anything, just pure text and collaboration from your terminal emulator.
def link(uri, label=None):
if label is None:
label = uri
parameters = ''
# OSC 8 ; params ; URI ST <name> OSC 8 ;; ST
escape_mask = '\033]8;{};{}\033\\{}\033]8;;\033\\'
return escape_mask.format(parameters, uri, label)
Call that function with link('https://example.com/') to get a simple, clickable link, or link('https://example.com/', 'example') for a custom label.
Note that links are faintly underlined in your terminal, and not all of them support the feature.
Yes, using some tools, like gNewt or Curses, you could create a button and 'on click' do an action (like open a browser to a given url).
gNewt : http://gnewt.sourceforge.net/
nCurses : https://docs.python.org/3.7/library/curses.html
Otherwise, it's the terminal application that will manage the text you give it, and if it doesn't implement uri's recognition your program won't work as you'd like.
No, some consoles do recognize urls and convert them to a clickable hyperlink. All you can do is make it easy to recognize for console applications by putting a http:// in your url.
Also see
How does bash recognize a link?

Interacting with pop-up boxes using selenium in python

I'm trying to use the Selenium module in python to generate a text list from one website, save it in a directory, and browse to that text list on another site to submit it.
I'm working on the script in two parts- 1. Get metadata and 2. Order data. I've successfully completed the script in part 1, except for the very last thing: Choosing to save the metadata file that was just generated. I left it alone to work on part 2, hoping I would stumble upon the answer, but I'm just reaching to same problem when the pop-up to choose file comes along.
In the documentation, I'm told that Selenium WebDriver has built-in support for handling popup dialog boxes and that after triggering a dialog box, if I call alert = driver.switch_to_alert() then I can "accept, dismiss, read its contents, or even type into a prompt."
However, it's not working. When I try alert.text('some text') or alert.send_keys(Keys.TAB), I keep getting the error NoAlertPresentException: Message: No alert is present and after adding the command to wait, I get the error TimeoutException: Message:
Are the popups I'm getting (screenshots attached) not recognized by Selenium? If so, how do I interact with them? It seems like using this to save and/or upload files is something that many people have to do, but I cannot find anything on Google. Specifically, I would like to choose 'Save File' then 'OK' for the first image and for the second I would like to browse to the file (i.e. enter the path into the file name field) and click 'Open.' I don't want to just change my Firefox settings to automatically save because this will eventually be run in a different environment, and that won't help solve my second problem.
Thanks!
EDIT:
I'm testing my script on windows but it will eventually be implemented on a linux cloud server. I thought I was going to have to switch to PhantomJS webdriver (which was probably going to make my problem worse) to do headless browsing but I found a way to keep firefox. I guess all this means is that I can't use AutoIT to fix my problem.
The popups you see are not regular popups that can be interacted with using switch_to. These popups are system dialogs and cannot be automated using selenium.
Usually people avoid having these dialogs shown in the first place by tweaking browser preferences, e.g.:
downloading file using selenium
Access to file download dialog in Firefox
How to download a file using Selenium's WebDriver?
For uploading, usually you can find the appropriate input element and send keys to it with a path to the file:
How to upload file ( picture ) with selenium, python
How to upload files into file inputs? (python-selenium docs)
Let me know if your case cannot be solved by using the answers in the links I've attached.
As for your first, "download file automatically" problem, you just need to set a correct content-type:
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', "application/xml,text/xml")
Second problem fix (upload part):
driver.find_element_by_name("input_product_list").send_keys(textpath)
driver.find_element_by_name('include_sr').click()
driver.find_element_by_id('submit').click()
Extremely simple implementation using AutoIT.
Below scripts and steps can hely you to click on 'Save>OK' on this window's pop-UP
Step 1: Download AutoIT package/tool here AutoIt You may select ZIP format (extract it)
Step 2: Open any text editor (say notepad) and copy below code and save with extension .au3
(e.g file.au3)
WinWait("[TITLE:Opening ; CLASS:MozillaDialogClass]","", 10)
If WinExists("[TITLE:Opening ; CLASS:MozillaDialogClass]") Then
WinActivate("[TITLE:Opening ; CLASS:MozillaDialogClass]")
Send("{DOWN}")
Sleep(20)
Send("{TAB}")
Sleep(20)
Send("{TAB}")
Sleep(20)
Send("{ENTER}")
EndIf
Step 3: From extracted zip (Step 1) look for folder named: Aut2Exe and open it
Step 4: Click Aut2exe_x64.exe if your OS is 64 bit otherwise click Aut2exe.exe
Step 5: Browse/Locate file created in Step2. (file saved as extension .a3)
AND Choose Destination (.exe/.a3x) and select .exe option (say file.exe)
AND then Click convert
Step 6: include this file.exe in your project folder and use it as per your requirement using below code (as it is in Eclipse):
driver.dwonload().click(); // it can be something else as per your flow
Runtime.getRuntime().exec("C:/*path_to_your_EXE_file(selected in step 6))*/file.exe");

Disable firefox save as dialog-selenium

I am web scraping with selenium and whenever i try to download i file the firefox download/save as file pops up however, even If i apply profile.set_preference('browser.helperApps.neverAsk.saveToDisk', "application/csv"), it still doesnt work, I have tried everyt .csv related MIME but doesn't work, is it possible to either click save as radio button and then click ok on the dialog or disable it entirely.
you should do two things, first set these three preferences as follows (this is in Java but I guess you manage to translate that to python :-):
profile.setPreference("browser.download.dir", "c:/yourDownloadDir");
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/csv, text/csv");
secondly, you should make sure the download file has the desired mime type. To do that, you can use the web developer tools and inspect the download.
EDIT:
To find out the MIME type open Chrome, press Ctrl+Shift+I (Cmd+Alt+I on Mac OS) change to the 'Network' tab and click your download link. You should see something like this:
Just an additional answer that might help someone, as comments to the accepted answer put me on the right track (thanks!). Another MIME type of CSV you might be dealing with is application/x-csv - that was my case and once I looked it up in the Network tab of the browser, I became a happier man :)
In C#
FirefoxOptions options = new FirefoxOptions();
options.SetPreference("browser.download.folderList", 2);
options.SetPreference("browser.download.manager.showWhenStarting", false);
options.SetPreference("browser.download.dir", "c:\\temp");
options.SetPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv");

Categories

Resources