Automate Google chrome - python

I hope you are ok.
I'm trying to make a script open chrome for me and then click on a tap :
import os
import subprocess
import pyautogui
cmd = 'google-chrome'
if (subprocess.call(cmd)):
pyautogui.click(618, 671)
#use it to know your screen position you may need to install scrot
# pyautogui.displayMousePosition()
but when I run the script I get an error, the error said :
[6869:6869:1111/121828.562003:ERROR:sandbox_linux.cc(374)] InitializeSandbox() called with multiple threads in process gpu-process.
[6815:6848:1111/121828.628223:ERROR:media_history_store.cc(363)] Failed to create or update the media history store.
Any ideas about how I can solve it.

subprocess.call(cmd) is a blocking call and your program will not continue.
Use subprocess.Popen(cmd) instead
Anyways to overcome that error, you should change your command to google-chrome --disable-gpu --disable-software-rasterizer
and don't run in sudo :)

Related

Kali Linux Vscode Python - Operation not permitted run python file with internal terminal

Hy guys, i am trying to use vscode in kali linux.
But I ran into a problem.
The moment I start the python file from the play symbol in the terminal it gives me a permission error.
But if I boot from the terminal using sudo python main.py it works fine.
Would you know how to help me fix this problem?
i am used to launch with play, i find it annoying to type in the terminal every time.
Screen error
I also tried the solution found on the internet of sudo code --user-data-dir = "~ / .vscode-root" my/path, but it doesn't solve the problem.
the code to execute:
import socket
import struct
import binascii
s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(0x0800))
while True:
print (s.recvfrom(2048))
Try to use :
sudo code
in the terminal
If it doesn't work try to remove it and install it again.

Triggering Python and py in Task Scheduler does not work

I create a task in task scheduler to open Python and run a code from a .py file and nothing happens.
Settings of my task:
General
Run whether user is logged in or not TICKED
Run with highest privileges TICKED
Actions
Program/script: C:\ProgramData\Anaconda3\python.exe
Add arguments: code.py (I have also tried "code.py")
Start in (location of my code.py): C:\Users\me\Downloads (I have also tried "C:\Users\me\Downloads")
Once I click Run, nothing happens. Can anyone advise please?
I was expecting that the scheduler will open Spyder, add code and run it. Not sure if this is possible or needed at all?
The process works. It seems that my python code was not working well.
If you are in doubt just run a simple code with your Task Scheduler:
e.g.
import webbrowser
url = 'https://www.bbc.co.uk/'
webbrowser.open(url)

Python automate open application

Please give me idea regarding how to tackle this problem. I am not able to find any resource regarding this. Your help will be immensely valuable. So we have one limited license software. And want to reiterate the python invoking the application. If the application gives the error that licence is not available it should close the application and wait for sometime say 1 min and again invoke the process, it should do so endlessly until a licence is available and the application is finally open.
I am able to open the application using
Import os
os.startfile('application executable')
After this I want the application to know if there is an error window popping , it should close the window and wait for sometime and again open the application
os.startfile returns as soon as the associated application is launch so use Popen instead.
As you are using windows use these steps.
To Open a Shortcut using Popen on Windows first install pywin32
Step one:
python -m pip install pywin32
Step two:
Navigate to your python Scrips folder something like
C:\Users\Name\AppData\Local\Programs\Python\Python38-32\Scripts then type the command.
pywin32_postinstall.py -install
Then the code to use Popen is.
import subprocess
import win32com.client, win32api
shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(r'path to shortcut')
long_path = shortcut.Targetpath
p = subprocess.Popen(long_path)
p.wait()

gTTS Python Script in background getting tcgetattr(): Inappropriate ioctl for device

Im developing an application on my raspberry Pi 3, using gTTS for Python:
from gtts import gTTS
import os
import threading
def greet_thread(word):
tts_thread = threading.Thread(target = greet, args=[word])
tts_thread.start()
def greet(word):
tts = gTTS(text=word, lang='es')
tts.save("words.mp3")
print 'Reproduciendo audio'
os.system("mpg321 -q presilence.mp3")
os.system("mpg321 -q words.mp3")
This works perfectly if i run the python script directly from a shell. But if i execute the python script in background using:
python -u script.py > log.txt 2>&1 &
i get this error in my log:
tcgetattr(): Inappropriate ioctl for device
and don't know why. I think is the way it's called from a background process, but no idea how to solved it. Thanks for your attention and help
The problem is that the program needs to be executed using the same user that executes the GUI. So if you are going to execute it in a command shell, avoid using 'root' user.
In my case i need the program executes on start up too. So i solved it using "auto start" instead of a crontab
Navigate to ~/.config/lxsession/LXDE-pi
nano autostart
Edit the file:
#lxpanel --profile LXDE-pi
#pcmanfm --desktop --profile LXDE-pi
//your script.sh
//or #python script.py
#xscreensaver -no-splash
#point-rpi
Save and exit
Reboot
I was having a similar issue with calling mpg321 from a python script that was launched from a bash script via crontab on reboot. I was getting the vague error: tcgetattr(): Inappropriate ioctl for device
After digging thru numerous threads and trying everything I could, I changed to use omxplayer instead and it seems to have solved the issue.
Best I can tell it was some kind of permissions issue with launching from crontab because I could run it without any issue from a terminal session.

Lock windows workstation using Python

Is there a way to lock the PC from a Python script on Windows?
I do not want to implement some kind of locking on my own - I'd like to use the same lock screen that's also used when the user presses WIN+L or locks the machine via the start menu.
This can be done with the LockWorkStation() function from user32.dll:
This function has the same result as pressing Ctrl+Alt+Del and clicking Lock Workstation.
In Python it can be called using using the ctypes/windll FFI from the Python stdlib:
import ctypes
ctypes.windll.user32.LockWorkStation()
A good solution that makes us avoid using Libraries/DLL files is to use the command prompet/ power shell.
try running this command in your cmd rundll32.exe user32.dll, LockWorkStation....The PC is Locked!!
so we can use subprocess to run this command like this:
import subprocess
cmd='rundll32.exe user32.dll, LockWorkStation'
subprocess.call(cmd)
One more solution :
Run command to install the necessary package
pip install pyautogui
Run the below code
import pyautogui
from time import sleep
pyautogui.hotkey('win', 'r')
pyautogui.typewrite("cmd\n")
sleep(0.500)
pyautogui.typewrite("rundll32.exe user32.dll, LockWorkStation\n")

Categories

Resources