Interact with website without sending keystrokes to window (Python, Windows)? - python

Is it possible to interact with a webpage loaded into a web browser (such as Chrome) without the window being active and without sending keystrokes to it? For example, suppose I have SoundCloud loaded in chrome and the chrome window minimized, but I want to create a hotkey on my computer (such as through Autohotkey) which acts as a play/pause button for the track. Would it be possible to have a Python script somehow interact with the browser to obtain that functionality without having to send it a keystroke?
The reason I'm trying to avoid having to send keystrokes is because it would require the Window to become briefly maximized and active. I can already do this in autohotkey. For example, I have an ahk script that iterates over all the windows, finds one with Soundcloud in the title, maximizes the window if it is minimized, sends the spacebar keystroke (which acts as play/pause on Soundcloud), and then minimizes the Window again if it was minimized to begin with.
This has the undesirable effect of making the Window flash briefly if it was minimized, or if virtual desktops are used, all the Windows flash if the Chrome window with Soundcloud is located on another virtual desktop other than the active one.
Ideally I could just write some program that runs silently in the background to send some kind of the request to the site that has the same effect as pressing the play/pause button without having to use the janky keystroke method I suggested above. But I am not sure if this is possible. What is actually happening when I click the play/pause button on Soundcloud, and is there some way write a program to get Chrome to do that without using keystrokes?
Any suggestions? I would prefer to do this without any browser plugins if possible.

Related

Python Selenium -- how to dismiss alert in endlessly loading window?

I'm trying to dismiss an alert, close a window, or suppress an alert or window from opening in the first place in Selenium (with Python), and I'm stuck with an infinitely loading window.
Screenshot of window loading
I'm working with the Selenium webdriver in Python, and when I enter text into one window's text box, in certain circumstances a new window automatically pops up with an alert in it. However, the window apparently loads infinitely. I do not need to interact with the window; I'd be fine if I could just close it out immediately.
But since the page is still loading, I cannot find a way to dismiss the alert or to force close the window. It just loads forever; the only thing I've been able to do is actually click the "OK" button myself, and this needs to work without manual user input.
Anyone know how I can either dismiss this alert or force the window to close? Or maybe suppress the window from opening or suppress the alert?
Thanks!
I've tried changing the page load timeout to then close the window or dismiss the alert, but that doesn't work. I cannot switch to the window and interact with it, and I cannot use driver.switch_to.alert.dismiss() or driver.close(); my code just hangs forever. I found that if I manually minimize the window, the alert is dismissed and I could probably interact with the window then, but I can't minimize the window with code; my code won't progress while the window is loading.
(Unfortunately this cannot be reproduced without someone using a private account to log into the system.)

How do I use win32gui and win32api to locate and move a window in windows, using python

This issue really arises from me not understanding what I have to pass to win32gui.EnumWindows()
http://timgolden.me.uk/pywin32-docs/win32gui__EnumWindows_meth.html
The short version of what Im trying to accomplish:
Detect a window that may be on any of 4 different monitors (my remote desktop window, not anything actually running on the remote desktop)
Move the window to a certain monitor (I can find the monitor HANDLE already)
Maximize the window
I would like to accomplish this via win32gui and win32api
I do however have a possible other method:
If anyone knows how to make pyautogui screenshot functions observe anything other than the primary display, then it would be possible to
Use pyautogui to detect the remote desktop window header (always the same, but could appear on 4 monitors, then click on it and drag to proper monitor and maximize with pyautogui.
and a third method:
Use pygetwindow to locate and bring to focus the remote desktop window, then use win32api to move that to the proper monitor. I would need a way to use win32 to get the HANDLE of the focused window.
EDIT: I was able to get WHAT I THOUGHT was the right handle using
pygetwindow.getAllTitles() to find the title of the remote desktop program, then using pygetwindow.getWindowsWithTitle('MyRemoteDesktopName - Remote Desktop Connection') since 'MyRemoteDesktopName - Remote Desktop Connection' was listed as the title before. This provided me with a handle that I used
win32gui.MoveWindow(197160, 0, 0, 1920, 1080, True) to move the window to my original screen. HOWEVER, the remote desktop bar did not move,a nd the screen, while it now fit, did not rescale the way dragging the bar to another window would have. Each of my monitors has a different size and resolution and my remote desktop auto scales to match whatever I drag the bar to. But when I use wMoveWindow, it disconnects from that bar

Create a delay between mouse clicks using Autokey

I am about to switch from Windows to Ubuntu. Since my mouse keeps doing multiple clicks each time I press the middle mouse button, I used AutoHotkey under Windows to add a delay after each click. This worked fine. Now under Ubuntu I want to use AutoKey to do the same. Autokey uses Python for its scripts though.
Here is the AutoHotkey script:
MButton::
If (A_TimeSincePriorHotkey < 200)
Return
Send {MButton}
Return
Currently, (as of version 0.95.4), this is not possible from within AutoKey, because it can’t handle mouse buttons as hotkeys.
This stackoverflow question may be of help: Triggering AutoKey Script via Mouse Button - How To?

Emulate a mouse click without using the actual mouse on linux

I am working with a program that collects a lot of data then shows it to you in the program. Unfortunately, the program is poorly designed and requires you to "approve" each bit of data collected manually by clicking a checkbox to approve it. In order to automate this process, I wrote a small script that scans for a checkbox, clicks it, then clicks "next item".
Unfortunately, this requires moving the actual mouse, meaning I can't use my computer until the program has finished. There are other questions that reference automating this with the winapi, however none of these work on Linux. What is a way to automate this on Linux?
You can simply start the program in a separate X server, for example using xvfb with
xvfb-run YOUR_PROGRAM
If you want to wrap just the instrumented program, that's possible too:
export DISPLAY=:42
Xvfb :42
THE_INSTRUMENTED_PROGRAMM
xdotool mousemove 1 1 click 1 # your instrumentation goes here

Display fullscreen jpeg on X Server (of Raspberry Pi) using Python 3

I am developing a software in Python that generates static jpeg files, which are written to the file system. The software ist running at startup and gets triggered by some external and internal events, generates the corresponding jpeg image and should show it in fullscreen on the connected HDMI-screen without any visible user control or menu bar.
There is no window manager (Gnome, KDE, ...) installed and running. The device is a passive one and remotely controlled.
I am currently starting a feh process in order to view an image, and kill it before the next one is shown. It works, but is not very satisfying.
Do you have any better idea how I can get my Python 3 program to display the jpeg on the X Server?
Do you really need an X-Server? If you don't any GUI or mouse, you may be better writing the pictures into the framebuffers without X-Server. You can simply use fbi for this.
Use feh with one of the --bg- options. This sets the background of the root X window, and then exits. So you don't have to deal with killing feh to restart it, just call the command every time you want to change the image.
feh --bg-scale image1.jpeg
This doesn't work with some desktop environments (kde, gnome, xfce, and others), but will work with lightweight window managers (evilwm, goomwwm, etc.) and will also work with no window manager.

Categories