I am trying out an utorrent automation using pywinauto lib. I want to add a torrent with URL. This option is under the file menu. I can get as far as opening uTorrent and then nothing happens. I used Swapy for generating this code. The box below opens only when I run the code in swapy. But when I save it into a file and run with cmd, only utorrent opens and a traceback occurs in the cmd.
from pywinauto.application import Application
app = Application().Start(cmd_line=u'"C:\\Users\\User\\AppData\\Roaming\\uTorrent\\u Torrent.exe" ')
torrentdfb = app[u'\xb5Torrent4823DF041B09']
torrentdfb.Wait('ready')
menu_item = torrentdfb.MenuItem(u'&File->Add Torrent from &URL...\tCtrl+U')
menu_item.Click()
app.Kill_()
Traceback:
Traceback (most recent call last):
File "AddTorrent.py", line 5, in <module>
torrentdfb.Wait('ready')
File "C:\Python27\lib\site-packages\pywinauto\application.py", line 380, in Wait
WaitUntil(timeout, retry_interval, lambda: self.__check_all_conditions(check_method_names))
File "C:\Python27\lib\site-packages\pywinauto\timings.py", line 308, in WaitUntil
raise err
pywinauto.timings.TimeoutError: timed out
I am new to python coding and I am not an expert. It would be helpful if you provide the explanation to solve my problem or the code. Thanks!!
uTorrent is spawning another process, this is how I got it:
>>> app.windows_()
[]
>>> app.process
6096
>>> app.connect(title_re=u'^μTorrent.*(build \d+).*')
<pywinauto.application.Application object at 0x000000000405C240>
>>> app.process
4044L
This is a final code working for me (with 32-bit uTorrent and 32-bit Python 2.7):
import pywinauto
app = pywinauto.Application().start(r'uTorrent.exe')
time.sleep(5) # because method connect() has no timeout param yet (planned for 0.6.0)
app.connect(title_re=u'^\u03bcTorrent.*(build \d+).*')
main_window = app.window_(title_re=u'^\u03bcTorrent.*(build \d+).*')
main_window.MenuSelect(u'&File->Add Torrent from &URL...\tCtrl+U')
app.AddTorrentFromURL.Edit.SetText('some URL')
app.AddTorrentFromURL.OK.Click()
Bitness is important. 32-bit uTorrent crashes if I use 64-bit Python.
Related
I was trying to set up MicroPython on PyCharm 2021.3.3, unfortunately I was not able to successfully connect my board to IDE. I'm getting trying to use tools>>MicroPython>>MicroPython REPL:
Found the device, but could not connect via port 'COM4': could not open port 'COM4': PermissionError(13, 'Odmowa dostępu.', None, 5)
I'm not sure what to suggest. :-(
Press ENTER to continue
Then I have tried to flush simple program to control despite previous error and it spits out:
Connecting to COM4
Traceback (most recent call last):
File "C:\Users\reczul\AppData\Roaming\JetBrains\PyCharm2021.3\plugins\intellij-micropython\scripts\microupload.py", line 139, in <module>
main(sys.argv[1:])
File "C:\Users\reczul\AppData\Roaming\JetBrains\PyCharm2021.3\plugins\intellij-micropython\scripts\microupload.py", line 56, in main
board = Pyboard(port)
File "C:\Users\reczul\PycharmProjects\pythonProject1\venv\lib\site-packages\ampy\pyboard.py", line 147, in __init__
raise PyboardError('failed to access ' + device)
ampy.pyboard.PyboardError: failed to access COM4
What can I do to fix this error?
The sample code I have used:
from machine import Pin, Timer
led = Pin(25, Pin.OUT)
tim = Timer()
def tick(timer):
global led
led.toggle()
tim.init(freq=2.5, mode=Timer.PERIODIC, callback=tick)```
Assuming you are using a Pi Pico.
You are most likely getting this error because you pressed the 'BOOTSEL' button while plugging your device in.
Drag your UF2 file onto the device, it should reboot.
Don't unplug the device and hold the 'BOOTSEL' this time.
It should be good to go now.
I am currently working with the Instabot API for python and I ran across the following issue:
I wrote a small program:
from instabot import Bot
bot = Bot()
bot.login(username = "[my username]", password = "[my passowrd]")
bot.follow("lego")
which worked fine after running it for the very first time. However, after running the program for a second time, this time following another account, it raised an error ("KeyError: ds_user").
This error can be fixed by deleting the config folder inside the project folder. Unfortunately, this isn't a very sustainable solution, as it makes working on the code really arduous. I therefore would like to know if there is any solution for getting the program to run multiple times without having to delete the config folder over and over again.
I am receiving the following traceback (code is running in an anaconda environment called "Instagram Automation"):
Traceback (most recent call last):
File "e:/Programme/OneDrive/Dokumente/Projekte/Instagram Automation/main.py", line 4, in <module>
bot.login(username = "[my username]", password = "[my password]")
File "E:\Programme\Anaconda\envs\Instagram Automation\lib\site-packages\instabot\bot\bot.py", line 443, in login
if self.api.login(**args) is False:
File "E:\Programme\Anaconda\envs\Instagram Automation\lib\site-packages\instabot\api\api.py", line 240, in login
self.load_uuid_and_cookie(load_cookie=use_cookie, load_uuid=use_uuid)
File "E:\Programme\Anaconda\envs\Instagram Automation\lib\site-packages\instabot\api\api.py", line 199, in load_uuid_and_cookie
return load_uuid_and_cookie(self, load_uuid=load_uuid, load_cookie=load_cookie)
File "E:\Programme\Anaconda\envs\Instagram Automation\lib\site-packages\instabot\api\api_login.py", line 352, in load_uuid_and_cookie
cookie_username = self.cookie_dict["ds_user"]
KeyError: 'ds_user'
As far as I can see, the only way on your side to fight the symptoms is to always delete the JSON file in the config folder, e.g:
import os
if os.path.isfile("path/to/config/file.json"):
os.remove("path/to/config/file.json")
import instabot
# rest of your code goes here
The developers of instabot should fix the source of the problem, for example by using self.cookie_dict.get("ds_user", "some default value") instead of self.cookie_dict["ds_user"]
This question already has answers here:
ModuleNotFoundError: What does it mean __main__ is not a package?
(6 answers)
Closed 4 years ago.
How to make screenshot from any url (web page)?
I was trying:
from .ghost import Ghost
ghost = Ghost(wait_timeout=4)
ghost.open('http://www.google.com')
ghost.capture_to('screen_shot.png')
Result:
No module named '__main__.ghost'; '__main__' is not a package
I was trying also:
Python Webkit making web-site screenshots using virtual framebuffer
Take screenshot of multiple URLs using selenium (python)
Fastest way to take a screenshot with python on windows
Take a screenshot of open website in python script
I've also tried other methods that are not listed here.
Nothing succeeded. Or an error or module is not found .. or or or.
I'm tired. Is there an easy way to make a screenshot of a web page using Python 3.X?
upd1:
C:\prg\PY\PUMA\tests>py save-web-html.py
Traceback (most recent call last):
File "save-web-html.py", line 2, in <module>
from .ghost import Ghost
ModuleNotFoundError: No module named '__main__.ghost'; '__main__' is not a package
upd2:
C:\prg\PY\PUMA\tests>py save-web-html.py
Exception ignored in: <bound method Ghost.__del__ of <ghost.ghost.Ghost object at 0x0000020A169CF860>>
Traceback (most recent call last):
File "C:\Users\Coar\AppData\Local\Programs\Python\Python36\lib\site-packages\ghost\ghost.py", line 325, in __del__
self.exit()
File "C:\Users\Coar\AppData\Local\Programs\Python\Python36\lib\site-packages\ghost\ghost.py", line 315, in exit
self._app.quit()
AttributeError: 'NoneType' object has no attribute 'quit'
Traceback (most recent call last):
File "save-web-html.py", line 4, in <module>
ghost = Ghost(wait_timeout=4)
TypeError: __init__() got an unexpected keyword argument 'wait_timeout'
In the late 80's this may have been a simple task, just render some html to an image instead of the screen.
But these days web-pages require client-side execution to build parts of its DOM and re-render based on client-side initiated AJAX (or equivalent) requests... it's a whole thing "web 2.0" thing.
Rendering a web-site such as http://google.com as a simple html return should be easy, but rendering something like https://www.facebook.com/ or https://www.kogan.com/ will have many back & fourth comms to display what you're expecting to see.
So restricting this to a pure python solution may not be plausible; I'm not aware of a python-based browser.
Consider running a separate service to take the screenshots, and use your core application (in python) to fetch requested screenshots.
I just tried a few with docker, many of them struggle with https and the aforementioned ajax behaviour.
earlyclaim/docker-manet appears to work demo page
edit: from your comments, you need the data from a graph that's rendered using a 2nd request.
you just need the json return from https://www.minnowbooster.net/limit/chart
try:
from urllib.request import urlopen # py3
except ImportError:
from urllib2 import urlopen # py2
import json
url = 'https://www.minnowbooster.net/limit/chart'
response = urlopen(url)
data_str = response.read().decode()
data = json.loads(data_str)
print(data)
When I try to import the serial I get the following error:
Traceback (most recent call last):
File "C:\Documents and Settings\eduardo.pereira\workspace\thgspeak\tst.py", line 7, in <module>
import serial
File "C:\Python27\lib\site-packages\serial\__init__.py", line 27, in <module>
from serial.serialwin32 import Serial
File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 15, in <module>
from serial import win32
File "C:\Python27\lib\site-packages\serial\win32.py", line 182, in <module>
CancelIoEx = _stdcall_libraries['kernel32'].CancelIoEx
File "C:\Python27\lib\ctypes\__init__.py", line 375, in __getattr__
func = self.__getitem__(name)
File "C:\Python27\lib\ctypes\__init__.py", line 380, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'CancelIoEx' not found
I have installed the latest version of pySerial, Python 2.7 runing on a WinXP laptop. Tried everywhere and found no similar problem. Is there any solution for that?
Thanks in advance...
The version of pySerial that you're using is trying to call a function that's only available in Windows Vista, whereas you're running Windows XP.
It might be worth experimenting with using an older version of pySerial.
The code in question was added to pySerial on 3 May 2016, so a version just prior to that might be a good start.
Older versions seem unavailable. But, this worked for me (assuming nanpy version 3.1.1):
open file \lib\site-packages\serial\serialwin32.py
delete methods _cancel_overlapped_io(), cancel_read(), cancel_write() in lines 436-455 nearly at the botton of the file
change method _close() als follows:
(Python)
def _close(self):
"""internal close port helper"""
if self._port_handle is not None:
# Restore original timeout values:
win32.SetCommTimeouts(self._port_handle, self._orgTimeouts)
# Close COM-Port:
if self._overlapped_read is not None:
win32.CloseHandle(self._overlapped_read.hEvent)
self._overlapped_read = None
if self._overlapped_write is not None:
win32.CloseHandle(self._overlapped_write.hEvent)
self._overlapped_write = None
win32.CloseHandle(self._port_handle)
self._port_handle = None
Additionally, create a non-default serial connection when starting the communication, otherwise you'll be bound to some linux device:
a = ArduinoApi(SerialManager("COM5:"))
for i in range(10):
a.pinMode(13, a.OUTPUT)
a.digitalWrite(13, a.HIGH)
# etc.
And in serial\win32.py comments
#CancelIoEx = _stdcall_libraries['kernel32'].CancelIoEx
#CancelIoEx.restype = BOOL
#CancelIoEx.argtypes = [HANDLE, LPOVERLAPPED]
There is no obvious reason why CancelIO was replaced with the cross-thread version CancelIOex, which allows code in one thread to cancel IO in another thread. Certainly cpython 2.x is single threaded.
To get pySerial to run on python 2.7 on Win2K, I just changed CancelIOex in serial back to CancelIO.
I'm recently started to use python with mobile app automation, as i decided to use python, the main instruments that I've found were monkeyrunner and androidviewclient.
But there is the first issue with which i dont know what to do:
package = 'com.mypackage.android'
activity = '.launchActivity'
component = package + "/" + activity
device, serialno = ViewClient.connectToDeviceOrExit()
device.startActivity(component=component)
time.sleep(3)
vc = ViewClient(device, serialno)
vc.dump()
showMenu = vc.findViewById("id/no_id/8")
showMenu.touch()
as i'm running it in windows cmd - monkeyrunner mypath\test-case1.py
i receive an exception:
131213 18:42:32.555:S [MainThread] [com.android.monkeyrunner.MonkeyRunnerOptions] Script terminated due to an exception
131213 18:42:32.555:S [MainThread] [com.android.monkeyrunner.MonkeyRunnerOptions]Traceback (most recent call last):
File "C:\Python27\tests\1.py", line 26, in <module>
device, serialno = ViewClient.connectToDeviceOrExit()
File "C:\Program Files (x86)\Android\AndroidViewClient\AndroidViewClient-maste
r\AndroidViewClient\src\com\dtmilano\android\viewclient.py", line 1381, in conne
ctToDeviceOrExit
ViewClient.setAlarm(timeout+5)
File "C:\Program Files (x86)\Android\AndroidViewClient\AndroidViewClient-maste
r\AndroidViewClient\src\com\dtmilano\android\viewclient.py", line 1341, in setAl
arm
signal.alarm(timeout)
File "C:\Program Files (x86)\Android\android-sdk\tools\lib\jython-standalone-2
.5.3.jar\Lib\signal.py", line 222, in alarm
NotImplementedError: alarm not implemented on this platform
am I doing something wrong? Please help.
Thank you a lot!
This is how setAlarm looks like
#staticmethod
def setAlarm(timeout):
osName = platform.system()
if osName.startswith('Windows'): # alarm is not implemented in Windows
return
signal.alarm(timeout)
so, it tries to identify that is Windows and then not invoking signal.alarm() which is not implemented, but for some reason it fails in your case.
Try to print the result of osName to see what went wrong.
UPDATE
Now I see, you are using monkeyrunner as the interpreter but AndroidViewClient >= 4.0.0 is 100% pure python, so you should run your scripts using a python 2.x interpreter.