Python how to wait for detect Ctrl+V and then continue - python

I am writing a python script that prepares a number of pieces of data to be posted into a web page and copies them to the clipboard using pyperclip. Currently I just have a time.sleep() statement between each one but based on latency and some slight variations in the web page response there may be other actions to be carried out (e.g. scroll down, clear a modal etc) before I am ready to paste the data. I run the risk of sometimes missing my 'window' for the paste but most of the time I am waiting for the sleep to finish and update the clipboard.
Instead of a fixed time period, it would be useful if I could make the python script pause until it detects the paste function and then load the clipboard with the next piece of data.
Note that the Ctrl+V will occur with the browser window having focus rather than the python script in case that makes a difference
I am using Chrome on Win-11 so an OS specific solution is fine for me. Also it does not need to be the Ctrl+V function (although this makes the most logical sense); any other trigger will do as long as it can't be confused and I can use pyautogui.keydown, keypress, keyup to trigger the Ctrl+V
Current Code:
pyperclip.copy(EventTitle)
os.system('cls')
print('Title: ' + EventTitle)
time.sleep(8)
os.system('cls')
pyperclip.copy(EventDescription)
print('Description: \n\n' + EventDescription)
time.sleep(8)
os.system('cls')
basically I want to replace the time.sleep() with {wait for paste command}

Related

Selenium Generated User Dialog

I have a selenium script that runs a loop on a process that prints text into my python IDLE to get a list of data, which I then copy and paste to excel - which is great. But not optimal.
I am currently using sleep at the start of the loop to give me time to change parameters on my trading view strategy tester, which gives me a different result to be printed as intended. However, I would like more or less time on most occasions. Waiting for the path to change does not work as the path never changes, it just gets looped again with a different text output based on the changes I would have made manually on the strategy settings.
Also worth noting that I am using firefox geckobrowser as opposed to chrome and that the purpose of the script is to automate a tedious manual task of data collection.
Is it at all possible to create a pop-up button or something of the like via selenium that says "Next" for example, that I can click and let the script know its okay for the loop to continue? Can I simply replace sleep with click "Next" or something of the like for example?
for i in range(20):
time.sleep(10)
netProfit = driver.find_element("xpath", '/html/body/div[2]/div[7]/div[2]/div[4]/div/div[2]/div/div[1]/div[1]/div[2]/div[2]')
numberTrades = driver.find_element("xpath", '/html/body/div[2]/div[7]/div[2]/div[4]/div/div[2]/div/div[1]/div[2]/div[2]')
winRate = driver.find_element("xpath", '/html/body/div[2]/div[7]/div[2]/div[4]/div/div[2]/div/div[1]/div[3]/div[2]')
print(netProfit.text, numberTrades.text, winRate.text)
This works for now, but it's a pain waiting for timer on occasions I don't need it and even more of a pain on occasions when it's not enough time.
I am expecting something like this to replace time.sleep(10)
WebDriverWait(driver, 180).until("Next" button is clicked)
Ideally I would like a solution that can be done in selenium as my experience is limited, but any workarounds would be appreciated all the same.

Make python restart when a selenium action takes too long to do

I do not have an error nor have one inside the selenium command prompt but for whatever reason sometimes whenever I get to a website (any website), selenium seems to get stuck on which ever random action (like get(), click(), send_keys() and such) its supposed to do making my shell process never end or progress but when I restart it manually, it seems to finally do what its supposed to do. I want to make my script as autonomous as possible and preset certain time window for each action so whenever this problem gets around, I do not need to come over and restart my script manually. I do not want to fix the long time it needs to process because it would probably mean sizing down on my proxies and arguments I gave which are crucial. I have a clue that this would probably work with the time.time() feature but implementing it seems hard.

Python script to close a specific window when it opens

I am searching for a way to close a specific window of a program immediately after it opens. Specifically, I am using the BUFF addon for Overwolf and after every game I play, this annoying overlay window opens up and blocks part of my screen. To close it windows Alt+Tab'ing out, I need to wait 10 seconds (that means if I'm not willing to subscribe to BUFF premium), and Alt+Tab'ing out after every game is just as annoying.
Is there any way that I can run a Python script while I'm playing that closes this window every time it opens immediately? I'm thinking of something that continously searches for a window with a specific name or maybe something that listens for the event of a window opening and then checking its name/title. I don't even know if Python can access these processes, but I guess it's worth to just ask.
And before I need to edit the post, I'm using Windows 11.

Automate to click 'down' key and take screenshot

I am newbie who wants to learn python. English is not my primary language, please ignore grammer error.
Here's my question.
In tradingview charts, i want to take screenshot, click down button, take screenshot again, wait for 3 second then go to next stock.
As i am newbie on python as well as stackoverflow if my any action fell dumb or wrong then please give me guidence.
Keyboard module (python) to control keyboard
first we need install a module name- keyboard in python
pip3 install keyboard
First, let's import the module:
import keyboard
Next, you can also simulate key presses using the send() function:
it will press space:
keyboard.send("space")
This will press and release the space button. In fact, there is an equivalent function press_and_release() that does the same thing.
You can also pass multi-keys:
keyboard.send("windows+d")
The + operator means we press both buttons in the same time, you can also use multi-step hotkeys:
send ALT+F4 in the same time, and then send space,
keyboard.send("alt+F4, space")
But what if you want to press a specific key but you don't want to release it ? Well, press() and release() functions comes into play:
press CTRL button
keyboard.press("ctrl")
release the CTRL button
keyboard.release("ctrl")
So this will press CTRL button and then release it, you can do anything in between, such as sleeping for few seconds, etc.
But now what if you want to write a long text and not just specific buttons ? send() would be inefficient. Luckily for us, write() function does exactly that, it sends artificial keyboard events to the OS simulating the typing of a given text, let's try it out:
keyboard.write("Python Programming is always fun!", delay=0.1)
Setting delay to 0.1 indicates 0.1 seconds to wait between keypresses, this will look fancy like in hacking movies!
Saving your problem
note- in windows 10, hot key for tacking screen shot is "windows+PrtScn.
Use your own hot key
from selenium import webdriver
import keyboard
import time
driver = webdriver.Chrome(executable_path="C:\\Users\\hp\\Desktop\\webcontrol\\chromedriver.exe")
driver.get("url ypu want to reach")
keyboard.send("windows+PrtScn")
keyboard.send("down")
keyboard.send("windows+PrtScn")
time.sleep(3)
driver.get("next stock url you want")
note- for going to next stock you can also use buttons or you can use selenium to click that particular stock
THANKS!

Python simulating 'enter' keypress in a game

So I've been attempting to get a script to type out a string of text in a video game (Guild Wars 2). Mainly I'm using pyautogui and for the most part it works fine. My issue is it seems I can't get the game to recognize 'enter'. For example if I have the code:
import pyautogui, time
time.sleep(2) #to allow me to have time to switch windows
pyautogui.press('enter')
pyautogui.typewrite("This is a test")
pyautogui.press('enter')
The two "press enter" function will not open and submit the text. If however I manually hit enter, the 3rd line types things out just fine.
I've also tried replacing press('enter') with keyDown followed by keyUp, but with still no results.
I've managed to create a workaround by having python hit F10, and then a separate Autohotkey script hitting enter when F10 is hit, but that is far from ideal. Are there any suggestions?
Extra note from comments: The script by itself works fine in other programs such as notepad. It seems to fail exclusively for the game client.
For anyone finding their way to this post. The answer is essentially that this can't be done in this fashion due to how most games interpret keypresses. A working system is shown here: Simulate Python keypresses for controlling a game
None of \n nor return works to me, so I have to use pydirectinput: https://github.com/learncodebygaming/pydirectinput
Install it using pip install pydirectinput, then import it and use as the README. Note that the file also needs to run as admin.

Categories

Resources