Using the module pywhatkit you can send messages on WhatsApp,
I used the script:
import pywhatkit as w
w.sendwhatmsg("xxxxxxxx", " this is a generated msg",9,26)
x is the number
The problem is, all it does is load the message in WhatsApp's textbox, it does not send. Am I missing something?
I just had the same issue, and I uninstalled and installed again pywhatkit with the following commands:
pip uninstall pywhatkit
pip install pywhatkit
I know this question is old, hope it will help someone with the same problem
import pywhatkit as w
import time
import pyautogui
import keyboard as k
w.sendwhatmsg("your number", 'hi', 8, 38)
pyautogui.click(1050, 950)
time.sleep(2)
k.press_and_release('enter')
by pyautogui.click you can adjust cursor to message box and then it will click on it
and after that with keyboard you can click enter as simple as that
you must increase the waiting time to more than or equal to 30 seconds, it happens generally due to slow internet. write it like this:
import pywhatkit as py
py.sendwhatmsg("+91xxxxxxxxxx", "hello", 13, 12, 32)
This seems to be a bug in Pywhatkit: https://github.com/Ankit404butfound/PyWhatKit/issues/20
the module currently only supports 1 screen, if you are using multiple screens and the new whatsapp web is currently opening on a separate window from where your code is running is going to give you that error. It is a known issue therefore if you are going to use more than one screen (who isn't) ensure your editor of choice (ie: google chrome and visual studio) are on the same screen.
After debugging sendwhatmsg function is doing the following things
opening your default browser using this query web.open(f"https://web.whatsapp.com/send?phone={phone_no}&text={quote(message)}")
clicking here pg.click(core.WIDTH / 2, core.HEIGHT / 2)
and pressing enter pg.press("enter")
The main problem is that this 2 variables core.WIDTH and core.HEIGHT gives you the resolution of your screen, not the resolution of the tab just opened in the browser. So if the browser tab its not maximized you might end up clicking somewhere else. So please make sure that your browser its maximized when opening.
The problem is that the window in which pywhatkit opens Whatsapp Web is not selected. When it opens Whatsapp Web, waits for a few seconds and clicks 'enter', the 'enter' is not processed by the browser window. Add the following code to your program:
# Import Libraries
import pywhatkit
import pyautogui
from tkinter import *
win = Tk() # Some Tkinter stuff
screen_width = win.winfo_screenwidth() # Gets the resolution (width) of your monitor
screen_height= win.winfo_screenheight() # Gets the resolution (height) of your monitor
print(screen_width, screen_height) # prints your monitor's resolution
pywhatkit.sendwhatmsg("+91xxxxxxxxxx", "Enter Message", 0, 0) # Sends the message
pyautogui.moveTo(screen_width * 0.694, screen_height* 0.964) # Moves the cursor the the message bar in Whatsapp
pyautogui.click() # Clicks the bar
pyautogui.press('enter') # Sends the message
No matter what screen you are using, pyautogui will always send your message except if Whatsapp updates its UI. In case this happens, then check where the message bar is located through pyautogui and whatever values you get, divide it by your screen's resolution. This way, it will work on any monitor.
you can do it like this :
import pywhatkit as kit
kit.sendwhatmsg('+966********','hi from Python Watsapp Bot',10,56)
Related
I have been searching everywhere on how to do this specifically but to no avail.
Let's say I want to get a list of all the open windows/apps on my device:
The next thing I want to do is to search for a window with a name "Notepad", or maybe "Facebook", then switch and maximize those window as the main window on the screen.
I've been using pyautogui module for switching tabs by automatically pressing alt + tab keys with the module. However, I believe it'll be prone to mistakes if I have no way of checking which tab/window is currently selected or maximized.
I was thinking of a solution that I can just continuously automate pressing alt + tab until the target window name is in the current active window, but I don't know how to get the name of the current active window as well.
Thank you for the help in advance.
I just found out that pyautogui has a method to get the active window title as well.
import pyautogui
import time
target_window = 'notepad'
count = 0
while True:
window_title = pyautogui.getActiveWindowTitle()
if target_window not in window_title:
count += 1
with pyautogui.hold('alt'):
for _ in range(0, count):
pyautogui.press('tab')
time.sleep(0.25)
Is there a way to clear the "Run" console in PyCharm?
I want a code that delete/hide all the print() made previously.
Like the "clear_all" button, but without having to press it manually.
I have read that there is a way to do it in a terminal with os.system("cls"), but in PyCharm, it only adds a small square without clearing anything.
Also, I don't want to use print("\n" *100) since I don't want to be able to scroll back and see the previous prints.
In Pycharm:
CMD + , (or Pycharm preferences);
Search: "clear all";
Double click -> Add keyboard shortcut (set it to CTRL + L or anything)
Enjoy this new hot key in your Pycharm console!
Pycharm Community Edition 2020.1.3
You can right click anywhere above the current line on the console, and choose the "Clear All" option. It'll clear the console
How to
Download this package https://github.com/asweigart/pyautogui. It allows python to send key strokes.
You may have to install some other packages first
If you are installing PyAutoGUI from PyPI using pip:
Windows has no dependencies. The Win32 extensions do not need to be
installed.
OS X needs the pyobjc-core and pyobjc module installed (in that
order).
Linux needs the python3-xlib (or python-xlib for Python 2) module
installed.
Pillow needs to be installed, and on Linux you may need to install additional libraries to make sure Pillow's PNG/JPEG works correctly. See:
Set a keyboard shortcut for clearing the run window in pycharm as explained by Taylan Aydinli
CMD + , (or Pycharm preferences);
Search: "clear all"; Double click ->
Add keyboard shortcut (set it to CTRL + L or anything)
Enjoy this new hot key in your Pycharm console!
Then if you set the keyboard shortcut for 'clear all' to Command + L use this in your python script
import pyautogui
pyautogui.hotkey('command', 'l')
Example program
This will clear the screen after the user types an input.
If you aren't focused on the tool window then your clear hot-key won't work, you can see this for yourself if you try pressing your hot-key while focused on, say, the editor, you won't clear the embedded terminals contents.
PyAutoGUI has no way of focusing on windows directly, to solve this you can try to find the coordinate where the run terminal is located and then send a left click to focus, if you don't already know the coordinates where you can click your mouse you can find it out with the following code:
import pyautogui
from time import sleep
sleep(2)
print(pyautogui.position())
An example of output:
(2799, 575)
and now the actual code:
import pyautogui
while True:
input_1 = input("?")
print(input_1)
pyautogui.click(x=2799, y=575)
pyautogui.hotkey('command', 'l')
Easy Method:
Shortcut: Control K,
Right click on terminal and clear Buffer
There's also another way of doing it using the system class from os. All you need to do is have this code:
from os import system, name
# define our clear function
def clear():
# for windows the name is 'nt'
if name == 'nt':
_ = system('cls')
# and for mac and linux, the os.name is 'posix'
else:
_ = system('clear')
# Then, whenever you want to clear the screen, just use this clear function as:
clear()
However, in order for this functionality to work in pycharm, you need to enable "Emulate terminal in output console". You can find this under edit configuration of the file where you want to use the clear function, then it's under Execution option. Here's a screenshot: pycharm screensho
You could just do a ("\n" * 100000000), so it'll be impossible to scroll back.
In PyCharm terminal you can type 'cls' just like in linux terminal.
For Python Console (where you see the output) assign a shortkey for "clear all" in File -> Settings -> Keymap -> Other -> "Clear all"
You can also click somewhere on the PythonConsole -> Right button -> clear.
Hope it helps
I just relised that instead of going to the trouble of setting up a shortcut, you could just set up a command using PyAutoGUI to click on the trash bin on the side of the window e.g
note, to install pyautogui click on the end of the import pyautogui line, then press alt+enter and click install pyautogui.
import pyautogui
# to find the coordinates of the bin...
from time import sleep
sleep(2) # hover your mouse over bin in this time
mousepos = pyautogui.position() gets current pos of mouse
x,y = mousepos # storing mouse position
print(mousepos) # prints current pos of mouse
# then to clear it;
pyautogui.click(x, y) # and just put this line of code wherever you want to clear it
(this isn't perfect thanks to the time it takes to run the code and using the mouse, but it is reasonable solution depending on what you are using it for.)
I hope this answer is helpful even though this is an old question.
Just click the trash can icon to the left of the command window and it clears the command history!
In PyCharm 2019.3.3 you can right click and select "Clear All" button.This is deleting all written data inside of the console and unfortunately this is manual.
Sorry to say this, here the main question is how to do it programmatically means while my code is running I want my code to clear previous data and at some stage and then continue running the code. It should work like reset button.
After spending some time on research I solved my problem using Mahak Khurmi's solution https://stackoverflow.com/a/67543234/16878188.
If you edit the run configuration you can enable "emulate terminal in output console" and you can use the os.system("cls") line and it will work normally.
Iconman had the easiest answer.
But simply printing "\n" * 20 (or whatever your terminal height is) will clear the screen, and the only difference is that the cursor is at the bottom.
I came here because I wanted to visually see how long each step of a complex process was taking (I'm implementing a progress bar), and the terminal is already full of scrolling logging information.
I ended up printing ("A" * 40) * 20, and then "B" and "C" etc., and then filming it. Reviewing the video made it easy to see how many seconds each step took. Yes I know I could use time-stamps, but this was fun!
A Python 3 and Gtk 3.22.5 application responds to a global key binding, thanks to the Keybinder library. Currently it invokes a simple callback:
def keybinder_callback(self, keystr, user_data):
print("Handling", keystr, user_data)
Elsewhere in the application is a Gtk.Window that contains a Gtk.MenuBar. The keybinder_callback needs to activate the menu bar as if the user had clicked the mouse on it.
(it's a very basic dock-type application rather than one with a typical application window)
I have tried sending a signal to a menu item:
self.menubar.get_children()[0].emit("activate-item")
without any joy. I also tried faking a button press
from Xlib.ext.xtest import fake_input
fake_input(display,X.ButtonPress,1)
which also had no effect, but feels like the wrong way to do it anyway. I thought sending a signal would be more appropriate.
Can a widget be activated programmatically as if a user mouse-clicked on it?
(it doesn't have to be a simulated mouse-click - it just needs to activate and focus the widget in the same way that a mouse-click would)
I have written an example that has a keybinder_callback like this:
def keybinder_callback(self, keystr, user_data):
print("Handling", keystr, user_data)
print("Event time:", Keybinder.get_current_event_time())
activate_the_menu()
I need to add some command to that function that will activate_the_menu.
I have tried many things, including capturing real events (by monitoring with xev) and simulating them ( ENTER_NOTIFY and FOCUS_CHANGE), by injecting them into Gdk.main_do_event.
I've tried calling menu.popup, menu.popup_at_widget, menubar.select_item and other numerous things. All to no avail.
Running out of ideas, I've even dusted off my old Xlib book...
Incidentally, whilst not a proper solution, this works from a shell:
$ xdotool mousemove 1605 10 click 1 mousemove restore
but not reliably from the keybinder_callback:
run("xdotool mousemove %d 10 click 1 mousemove restore" %
(self.get_position().root_x+5) , shell=True)
After trying many things, I found that this works.
import Xlib
from Xlib import X
from Xlib.display import Display
from Xlib.ext.xtest import fake_input
import time
def keybinder_callback(self, keystr, user_data):
time.sleep(0.2)
x = self.get_position().root_x+5
display = Display()
mpos = display.screen().root.query_pointer()._data
display.screen().root.warp_pointer(x,5)
display.sync()
fake_input(display,X.ButtonPress,1, X.CurrentTime, X.NONE, x, 5)
display.sync()
fake_input(display,X.ButtonRelease,1)
display.screen().root.warp_pointer(mpos['root_x'],
mpos['root_y'])
display.sync()
I found that the time delay was necessary to avoid this error:
Gdk-CRITICAL **: Window 0x1e7c660 has not been made visible in GdkSeatGrabPrepareFunc
I am attempting to complete a simple process of opening a web/browser based document, selecting a field within said document, and then copying it so that it goes into my operating system's clipboard. Here's the specs :
Windows 7
Google Chrome ( latest stable )
Python 3.5
pyautogui for keyboard/mouse control
Here is the field I am trying to work with ( http://screencast.com/t/jt0kTagb ). When that little arrow is clicked it pops open to reveal a calendar to pick a date. If you click directly in the field instead it highlights the field's contents. When I manually press CTRL+C in this situation the field's contents go right into the clipboard as expected.
I've tried two methods of getting the field to go into my clipboard. The first was leveraging pyautogui's keyDown/up and press functions which essentially looked like :
imageCoord = noClick("img/date.png")
x, y = pyautogui.center(imageCoord)
pyautogui.click(x, y + 20)
pyautogui.keyDown('ctrl')
pyautogui.press('c')
pyautogui.keyUp('ctrl')
I then attempted to just use the app menu that appears if you right click on something which looked like this:
imageCoord = noClick("img/date.png")
x, y = pyautogui.center(imageCoord)
pyautogui.click(x, y + 20, button='right')
pyautogui.press("down", presses=2)
time.sleep(1)
pyautogui.press('enter')
Lastly I tried the pyautogui.hotkey() function which looked like this :
imageCoord = noClick("img/date.png")
x, y = pyautogui.center(imageCoord)
pyautogui.click(x, y + 20, button='right')
pyautogui.hotKey('ctrl', 'c')
In all three events the field is indeed selected and as best as I can tell the keypresses are going through as all other presses/functions that happen prior go off without a hitch.
The problem that I am facing is that when I do this manually in the same fashion as both of those scripts above I am able to get the contents. When I use the scripts, the clipboard is never updated/populated with the field's contents. Is there something I am overlooking or not considering when working with Python and Window's clipboard?
In the end all I am trying to do is put that value into an excel sheet. Any advice would be appreciated!
I have also discovered this issue on a different automation script, and have been working on troubleshooting it for several days. I'm also on Python 3.5 and Windows 7. I can rule out that it has anything to do with Google Chrome, as my particular script is actually working with SAP.
The documentation for pyautogui on Read the Docs (https://pyautogui.readthedocs.io/en/latest/cheatsheet.html#keyboard-functions) gives a direct example of using Ctrl + C to copy text to the clipboard, so I can verify you're not actually doing something wrong. I believe you're just looking at a bug here.
I have opened an issue on the project's GitHub page:
https://github.com/asweigart/pyautogui/issues/102
Use the PyAutoGui module.
pip install PyAutoGUI
We can easily use HotKey combinations.
See docs: https://pyautogui.readthedocs.io/en/latest/keyboard.html#the-hotkey-function
Pressing Ctrl+C
>>> import pyautogui
>>> pyautogui.hotkey('ctrl', 'c')
I found the solution!
pyautogui.keyDown('ctrl')
pyautogui.keyDown('c')
pyautogui.keyUp('c')
pyautogui.keyUp('ctrl')
In my script I had to use root.update() after.
I need to do some macros and I wanna know what is the most recommended way to do it.
So, I need to write somethings and click some places with it and I need to emulate the TAB key to.
I do automated testing stuff in Python. I tend to use the following:
http://www.tizmoi.net/watsup/intro.html
Edit: Link is dead, archived version: https://web.archive.org/web/20100224025508/http://www.tizmoi.net/watsup/intro.html
http://www.mayukhbose.com/python/IEC/index.php
I do not always (almost never) simulate key presses and mouse movement. I usually use COM to set values of windows objects and call their .click() methods.
You can send keypress signals with this:
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys("^a") # CTRL+A may "select all" depending on which window's focused
shell.SendKeys("{DELETE}") # Delete selected text? Depends on context. :P
shell.SendKeys("{TAB}") #Press tab... to change focus or whatever
This is all in Windows. If you're in another environment, I have no clue.
Maybe you are looking for Sendkeys?
SendKeys is a Python module for
Windows that can send one or more
keystrokes or keystroke combinations
to the active window.
it seems it is windows only
Also you have pywinauto (copied from my SO answer)
pywinauto is a set of open-source
(LGPL) modules for using Python as a
GUI automation 'driver' for Windows NT
based Operating Systems (NT/W2K/XP).
and example from the web page
> from pywinauto import application
> app = application.Application.start("notepad.exe")
> app.notepad.TypeKeys("%FX")
> app.Notepad.MenuSelect("File->SaveAs")
> app.SaveAs.ComboBox5.Select("UTF-8")
> app.SaveAs.edit1.SetText("Example-utf8.txt")
> app.SaveAs.Save.Click()
pyautogui is a great package to send keys and automate several keyboard / mouse related tasks. Check out Controlling the Keyboard and Mouse with GUI Automation and PyAutoGUI’s documentation.
You can use PyAutoGUI library for Python which works on Windows, macOS, and Linux.
Mouse
Here is a simple code to move the mouse to the middle of the screen:
import pyautogui
screenWidth, screenHeight = pyautogui.size()
pyautogui.moveTo(screenWidth / 2, screenHeight / 2)
Docs page: Mouse Control Functions.
Related question: Controlling mouse with Python.
Keyboard
Example:
pyautogui.typewrite('Hello world!') # prints out "Hello world!" instantly
pyautogui.typewrite('Hello world!', interval=0.25) # prints out "Hello world!" with a quarter second delay after each character
Docs page: Keyboard Control Functions.
More reading: Controlling the Keyboard and Mouse with GUI Automation (Chapter 18 of e-book).
Related questions:
Python GUI automation library for simulating user interaction in apps.
Python simulate keydown.
Two other options are:
pynput - https://pypi.org/project/pynput/ - which is for Windows (tested), Linux and MacOS- docs are at https://pynput.readthedocs.io/en/latest/
PyDirectInput - https://pypi.org/project/PyDirectInput/ - which is for Windows only and can be used with (or without) PyAutoGUI
Warning - if you are wanting to use keyboard control for games, then pynput doesn't always work - e.g. it works for Valheim, but not for the Witcher 3 - which is where PyDirectInput will work instead. I also tested PyDirectInput and it works for Half life 2 (as a test of an older game).
Tip - You will likely need to reduce (don't remove for games) the delay between character typing - use pydirectinput.PAUSE = 0.05
As an example, here is a function that allows virtual keyboard typing - currently only tested on Windows:
from pynput import keyboard
try:
import pydirectinput
pydirectinput.PAUSE = 0.05
except ImportError as err:
pydirectinput = False
print("pydirectinput not found:")
def write_char(ch):
upper = ch.isupper()
if pydirectinput and pydirectinput.KEYBOARD_MAPPING.get(ch.lower(), False):
if upper:
pydirectinput.keyDown('shift')
print('^')
pydirectinput.write(ch.lower(), interval=0.0)
print(ch)
if upper:
pydirectinput.keyUp('shift')
else:
keyboard.Controller().type(ch)
This allows a string to be sent in, with upper case alphabetic characters handled through pydirectinput. When characters don't simply map, the function falls back to using pynput. Note that PyAutoGUI also can't handled some shifted characters - such as the £ symbol, etc.