Skip to content

解决弹窗验证码输入问题 #466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion easytrader/config/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ class HTZQ(CommonConfig):


class UNIVERSAL(CommonConfig):
DEFAULT_EXE_PATH = r"c:\\ths\\xiadan.exe"
# DEFAULT_EXE_PATH = r"c:\\ths\\xiadan.exe"
DEFAULT_EXE_PATH = r"C:\同花顺软件\同花顺\xiadan.exe"

BALANCE_CONTROL_ID_GROUP = {
"资金余额": 1012,
Expand Down
16 changes: 13 additions & 3 deletions easytrader/grid_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
from typing import TYPE_CHECKING, Dict, List, Optional

import pandas as pd
import pywinauto.keyboard
import pywinauto
import pywinauto.clipboard
import pywinauto.keyboard

from easytrader.log import logger
from easytrader.utils.captcha import captcha_recognize
from easytrader.utils.captcha import captcha_recognize, get_yzm_from_image
from easytrader.utils.win_gui import SetForegroundWindow, ShowWindow, win32defines

if TYPE_CHECKING:
Expand Down Expand Up @@ -184,7 +184,17 @@ def get(self, control_id: int) -> List[Dict]:
count = 10
while count > 0:
if self._trader.is_exist_pop_dialog():
break
# 这里有可能是要求输入验证码的弹窗 liudongming
image = self._trader.app.top_window().capture_as_image()
yzm = get_yzm_from_image(image)
if len(yzm) == 4:
# self._trader.app.top_window().print_control_identifiers()
self._trader.app.top_window().Edit.type_keys(yzm)
self._trader.app.top_window().type_keys("{ENTER}", set_foreground=False)

grid.type_keys("^s", set_foreground=False)
else:
break
self._trader.wait(0.2)
count -= 1

Expand Down
3 changes: 3 additions & 0 deletions easytrader/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def post_prepare():
user = api.use(json_data.pop("broker"))
user.prepare(**json_data)

# AttributeError: 'RemoteClient' object has no attribute 'enable_type_keys_for_editor'
user.enable_type_keys_for_editor()

global_store["user"] = user
return jsonify({"msg": "login success"}), 201

Expand Down
20 changes: 12 additions & 8 deletions easytrader/universal_clienttrader.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def login(self, user, password, exe_path, comm_password=None, **kwargs):
path=self._run_exe_path(exe_path), timeout=1
)
# pylint: disable=broad-except
except Exception:
except Exception as e:
print(e)
self._app = pywinauto.Application().start(exe_path)

# wait login window ready
Expand All @@ -41,15 +42,18 @@ def login(self, user, password, exe_path, comm_password=None, **kwargs):
except:
self.wait(1)

self.wait(1)
self._app.window(handle=login_window).Edit1.set_focus()
self._app.window(handle=login_window).Edit1.type_keys(user)
try: # ldm
self.wait(1)
self._app.window(handle=login_window).Edit1.set_focus()
self._app.window(handle=login_window).Edit1.type_keys(user)

self._app.window(handle=login_window).button7.click()
self._app.window(handle=login_window).button7.click()

# detect login is success or not
# self._app.top_window().wait_not("exists", 100)
self.wait(5)
# detect login is success or not
# self._app.top_window().wait_not("exists", 100)
self.wait(5)
except Exception as e:
print(e)

self._app = pywinauto.Application().connect(
path=self._run_exe_path(exe_path), timeout=10
Expand Down
21 changes: 21 additions & 0 deletions easytrader/utils/captcha.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,24 @@ def invoke_tesseract_to_recognize(img):
)
valid_chars = re.findall("[0-9a-z]", res, re.IGNORECASE)
return "".join(valid_chars)


def get_yzm_from_image(image: Image):
"""
liudongming
image是整个验证码弹窗的截图
:param image:
:return:
"""
left = 188
top = 89
code = ''
rangle = (left, top, left + 71, top + 23)
try:
yzm = image.crop(rangle)
yzm.save(r'C:\yzm.jpg')
code = invoke_tesseract_to_recognize(r'C:\yzm.jpg')
print(f'验证码:{code}')
except Exception as e:
print(f'get_yzm_from_image 异常:{e}')
return code