How to refresh a browser tap after time.sleep in python - python

So this is what I'm using to open the browser
import webbrowser
import time
url = "http://google.com"
time = 5
def TestBrowse(url,time):
webbrowser.open(url)
time.sleep(time)
I want a function or method following time.sleep that will refresh the tab that the function opens. This is a module I'm just getting familiar with so I don't even know if its a better module or solution for this (or if at all possible)
Infact my main target was to be able to close the tab but I've been reading there is no way to do that, If this is false I would also love knowing how to do that. I've experimented with using os.system to kill the browser but os.system never seems to work inside a function (and It doesn't seem like a good idea anyway)

Maybe using selenium would be a better option for browser programing.
It accepts python scripts.
Also, you could try creating a wrapper web page with an embedded script that does the refreshing and/or exiting, though the browser might threat it as a cross-site scripting and limit the functionality of the URL you are trying to access. In any case to use that you would need to program in javascript rather than python.

Related

How can I send keystrokes and mouse movement to a specific PID?

How can I send keystrokes and mouse movements to a specific running program through its PID. I've used both pywinauto and pynput, and they work great, but I want to send keys to a program that is not in focus. I found this question: How to I send keystroke to Linux process in Python by PID? but it never explains what filePath is a path to.
If you could help solve for this example, that would be great! I want to send the "d" key to an open Minecraft tab for 10 seconds, and then send the "a" key for the next 10 seconds and stop. I would need this to be able to run in the background, so it could not send the keys to the computer as a whole, but only to the Minecraft tab. I am on Windows 10 by the way.
Any help would be appreciated!
Pretty sure you won't be able to, at least not easily let me explain a little bit how all of this works.
Lets start with the hardware and os, the OS has certain functions to read the input you give the computer. This input goes into a "pipe", the OS is reading input, and putting into the pipe, on the other side of the pipe there may be an application running, or it may not. The OS typically manages this (which app to put on the pipe listening) by defining which app/window is active. Apps access this pipe with the API given by the OS, they read the input and decide on it.
The libraries you cited above, change the values of the keyboard and mouse, in other words, they make the OS read other values, not the real ones, then the OS puts them in the "pipe", and are read by the app that is listening on the pipe (the one active). Some apps have their own API's for this, but I would guess Minecraft doesn't. If they don't have an API, what can you do? well, as I said, nothing easy, first of all "hacking" the app, in other words change it to listen to some other input/output rather than the one given by the OS, (this would be you making your own API). The other one would be you changing the OS, which would also be extremely hard, but maybe a tiny bitty easier. It also depends on your OS, I think Microsoft does offer input injection api's
So, simple options, first, run a VM with a GUI and use pywinauto, pyautogui, etc. The other option would be if you can run it in the browser, do so, and use something like Selenium to automate the input.
Quick note, why does selenium works and the browser can read input in the background? Easy, it's not, it just executes the code it would execute if it would have read the input! javascript, cool isn't
With ahk you can do this with Python+AutoHotkey
pip install ahk
pip install "ahk[binary]"
from ahk import AHK
from ahk.window import Window
ahk = AHK()
win = Window.from_pid(ahk, pid='20366')
win.send('abc') # send keys directly to the window
Note that some programs may simply ignore inputs when they are not in focus. However, you can test this works in general even when not in focus by testing with a program like notepad
Full disclosure: I author the ahk library.

Unable to close browser using webbrowser module

Unable to close browser using python webbrowser.
I don't know how to close it.
Code is:
elif 'open chrome' in query:
Cpath = 'C:\\Users\\hi\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe'
os.startfile(Cpath)
You do not appear to using Python's webbrowser module to start one up, although even if you did it wouldn't help because the controller objects it creates don't support a close() method.
That's also true about the os.startfile() function you are using, and there's no option to wait for the application to close or to force it to do so.
Because of that, I suggest that you create an instance of the subprocess.Popen class to start the web-browser application, because if you did, you would then have the option of calling the terminate() (or kill()) method of the class instance to close it.

Why do I get a "Can't pickle ..." when I try to run a function and kill it if it timeouts?

first of all let me tell you I'm kind of a newb at threads and multiprocessing.
I've got a Python graphical program that once you choose a translation site and click a button, calls a thread to open up a browser and do a search on that given site (I'm using splinter, which uses selenium, and qt for the graphical part) of the terms located in a text file selected by the user.
I'm trying to timeout the search of each term on the site to 30 seconds, because sometimes there are loading problems, or other issues, and I want to be able to skip the term if it takes to time to search and try at a later time.
So:
I have this code on the event handler of the button:
self.searchThread = PesquisaThread()
self.searchThread.start()
Then at the run method of the thread I have:
glossary.do_search(0, site_to_look_for)
'glossary' is the class of the gui.
Then if the glossary translib is selected 'do_search' does:
search_translib(language_from,language_to)
Which then calls for the function that does the opening of the browser and clicking of the links, etc, like so...
p = multiprocessing.Process(target=search_term_language,args=(browser,'Portuguese', row, column,termo_unicode))
p.start()
p.join(30)
But when this particular code runs I get:
pickle.PicklingError: Can't pickle '_subprocess_handle' object: <_subprocess_handle object at 0x0230DA60>
I'm at a loss, does anyone know what is going on.
Thank you
Daniel

How to open an application in Python, and then let the rest of the script run?

I have been trying to create a script which reloads a web browser called Midori if the internet flickers. But, it seems only to work if I open Midori through the CLI - otherwise, the program crashes after I reload it. I have decided that the best idea is thus to have the script open Midori through the subprocess module. So, I put this as one of the first arguments in my code:
import subprocess as sub
sub.call(["midori"])
The browser opens, but the rest of the program freezes until I quit Midori. I have tried to use threading, but it doesn't seem to work.
Is there any way to open an application through Python, and then let the rest of the script continue to run once said application has been opened?
From the docs:
Run the command described by args. Wait for command to complete, then return the returncode attribute.
(Emphasis added)
You can see this is the behaviour we should expect. To get around this, use subprocess.Popen instead. This will not block in the same way:
from subprocess import Popen
midori_process = Popen(["midori"])

Calling a python program from an external link and communicating with it

I have no idea if this has an official name, nor do I know a better way to really describe what I'm going for here so I'll list the two things I'd like to accomplish.
In a nutshell I have a task management program that emails people when they've been assigned a task. What I'd like is some way to generate a link (html or otherwise) that can be clicked that will either:
Launch the program with some command-line arguments, or
If the program is already open, communicate some commands to it, so that it could be, perhaps, set as the top window and then it could switch to the task.
I know this must be possible, but I have not yet gleaned the knowledge of how to communicate with programs outside of themselves. Any ideas? Thanks in advance!
I use tools like itty and flask to implement this kind of functionality.
For example, using itty here's how you could launch a program with some command-line arguments:
from itty import *
#post('/someprogram/(?P<args>\w+)')
def launcher(request, args):
'Launch a program with some command-line arguments'
return subprocess.check_output('someprogram', shlex.split(args))
run_itty()
The front-end for this could be a simple web page with an html form to get command-line arguments and a submit button to make the POST request.
This approach works equally well over a local network or over the web.

Categories