How to simulate key press in a specific chrome tabwindow

Question:

So I want to simulate key presses in a SPECIFIC windowchrome tab. For eg, with pyautogui:

import pyautogui as py

while True:
    py.press("1")
    py.press("space")
    py.press("4")

This works when I’m on a tab, but if I go to another tab, it will simulate the key presses there. I want it to only simulate key presses in a specific tab, even if I’m in another tab.
Is there a way to modify the above program to do this, or is there another pythonic way to achieve this?

Asked By: Raed Ali

||

Answers:

Unfortunately, PyAutoGUI can’t do this because it only blindly clicks on the screen at x, y coordinates. Unless you know the coordinates of the tab, you won’t be able to put it in focus and send key presses to the window.

I recommend a library like Selenium for doing GUI automation in web browsers.

Answered By: Al Sweigart

It will select second tab. Like that, which tab we need we can select.

import pyautogui
whichtab = 2
pyautogui.hotkey(‘ctrl’,f'{whichtab}’)

Answered By: Kathiravan.S
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.