From cdd69683a9b0579dca0d56fad56062afa04ee460 Mon Sep 17 00:00:00 2001 From: clericpy Date: Tue, 14 Jul 2020 00:31:23 +0800 Subject: [PATCH 1/7] typo --- ichrome/__main__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ichrome/__main__.py b/ichrome/__main__.py index a96e06a..4b526a5 100644 --- a/ichrome/__main__.py +++ b/ichrome/__main__.py @@ -129,7 +129,7 @@ def main(): action="store_true") parser.add_argument("-C", "--clear", - "--clear", + "--clean", dest='clean', help="clean user_data_dir", default=False, From 12e37d77007ec77bf06724e1387a0162a3736904 Mon Sep 17 00:00:00 2001 From: clericpy Date: Wed, 22 Jul 2020 23:15:19 +0800 Subject: [PATCH 2/7] register loading event before seting url --- ichrome/async_utils.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/ichrome/async_utils.py b/ichrome/async_utils.py index 2b843f5..aa1d5e4 100644 --- a/ichrome/async_utils.py +++ b/ichrome/async_utils.py @@ -5,7 +5,6 @@ import json import re import time -import traceback from asyncio.base_futures import _PENDING from asyncio.futures import Future from base64 import b64decode @@ -1044,6 +1043,14 @@ async def set_url(self, Navigate the tab to the URL. If stop loading occurs, return False. """ logger.debug(f'[set_url] {self!r} url => {url}') + if timeout == 0: + # no need wait loading + loaded_task = None + else: + # register loading event before seting url + loaded_task = asyncio.ensure_future( + self.wait_loading(timeout=timeout, + timeout_stop_loading=timeout_stop_loading)) if url: self._url = url if referrer is None: @@ -1058,8 +1065,7 @@ async def set_url(self, else: data = await self.reload(timeout=timeout) # loadEventFired return True, else return False - return bool(data and (await self.wait_loading( - timeout=timeout, timeout_stop_loading=timeout_stop_loading))) + return bool(data and loaded_task and (await loaded_task)) async def js(self, javascript: str, From 8d5073cc165d7b1a4823d4db552b2f2332a2a6e2 Mon Sep 17 00:00:00 2001 From: clericpy Date: Wed, 22 Jul 2020 23:19:18 +0800 Subject: [PATCH 3/7] set default port 9222 for debugger --- ichrome/debugger.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ichrome/debugger.py b/ichrome/debugger.py index b5b9728..02ceb38 100644 --- a/ichrome/debugger.py +++ b/ichrome/debugger.py @@ -226,12 +226,12 @@ def connect_a_chrome(host='127.0.0.1', port=None, **daemon_kwargs) -> Chrome: return Chrome(host=host, port=d.port) -def get_a_tab(host='127.0.0.1', port=None, **daemon_kwargs) -> AsyncTab: +def get_a_tab(host='127.0.0.1', port=9222, **daemon_kwargs) -> AsyncTab: chrome = connect_a_chrome(host=host, port=port, **daemon_kwargs) return chrome.get_tab() -def get_a_new_tab(host='127.0.0.1', port=None, **daemon_kwargs) -> AsyncTab: +def get_a_new_tab(host='127.0.0.1', port=9222, **daemon_kwargs) -> AsyncTab: chrome = connect_a_chrome(host=host, port=port, **daemon_kwargs) return chrome.new_tab() From 241a581b947cd0b7e3a235e57550beeb3f13f6fe Mon Sep 17 00:00:00 2001 From: clericpy Date: Wed, 22 Jul 2020 23:28:14 +0800 Subject: [PATCH 4/7] add mouse_click_element --- ichrome/async_utils.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/ichrome/async_utils.py b/ichrome/async_utils.py index aa1d5e4..ee0476a 100644 --- a/ichrome/async_utils.py +++ b/ichrome/async_utils.py @@ -1496,7 +1496,9 @@ async def click(self, timeout=timeout) async def get_element_clip(self, cssselector: str, scale=1, timeout=NotSet): - """Element.getBoundingClientRect""" + """Element.getBoundingClientRect. + {"x":241,"y":85.59375,"width":165,"height":36,"top":85.59375,"right":406,"bottom":121.59375,"left":241} + """ js_str = 'JSON.stringify(document.querySelector(`%s`).getBoundingClientRect())' % cssselector rect = await self.js(js_str, timeout=timeout, @@ -1646,6 +1648,31 @@ async def keyboard_send(self, timeout=timeout, **kwargs) + async def mouse_click_element(self, + cssselector: str, + button='left', + count=1, + scale=1, + multiplier=0.5, + timeout=NotSet): + # dispatchMouseEvent on selected element center + rect = await self.get_element_clip(cssselector, + scale=scale, + timeout=timeout) + if rect: + x = rect['x'] + multiplier * rect['width'] + y = rect['y'] + multiplier * rect['height'] + await self.mouse_press(x=x, + y=y, + button=button, + count=count, + timeout=timeout) + return await self.mouse_release(x=x, + y=y, + button=button, + count=1, + timeout=timeout) + async def mouse_click(self, x, y, button='left', count=1, timeout=NotSet): await self.mouse_press(x=x, y=y, From 23fd7e9fbccb04a1ec1786faae22f00d67fff4ac Mon Sep 17 00:00:00 2001 From: clericpy Date: Thu, 23 Jul 2020 00:01:14 +0800 Subject: [PATCH 5/7] rename mouse_click_element_rect and add test case --- examples_async.py | 6 +++++- ichrome/async_utils.py | 17 ++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/examples_async.py b/examples_async.py index 7d04c3b..e03e770 100644 --- a/examples_async.py +++ b/examples_async.py @@ -228,7 +228,7 @@ async def test_tab_set_ua_headers(tab: Tab): async def test_tab_keyboard_mouse(tab: Tab): if 'httpbin.org/forms/post' not in (await tab.current_url): await tab.set_url('http://httpbin.org/forms/post', timeout=5) - rect = await tab.get_bounding_client_rect('[type="email"]') + rect = await tab.get_bounding_client_rect('[type="tel"]') await tab.mouse_click(rect['left'], rect['top'], count=1) await tab.keyboard_send(text='1') await tab.keyboard_send(text='2') @@ -237,6 +237,10 @@ async def test_tab_keyboard_mouse(tab: Tab): await tab.mouse_click(rect['left'], rect['top'], count=2) selection = await tab.get_variable('window.getSelection().toString()') assert selection == '123123' + # test mouse_click_element + await tab.mouse_click_element_rect('[type="tel"]') + selection = await tab.get_variable('window.getSelection().toString()') + assert selection == '' # test mouse_drag_rel_chain draw a square, sometime witeboard load failed..... await tab.set_url('https://zhoushuo.me/drawingborad/', timeout=5) await tab.mouse_click(5, 5) diff --git a/ichrome/async_utils.py b/ichrome/async_utils.py index ee0476a..a4f1b2a 100644 --- a/ichrome/async_utils.py +++ b/ichrome/async_utils.py @@ -878,8 +878,7 @@ async def get_response( {'id': 2, 'result': {'body': 'source code', 'base64Encoded': False}} some ajax request need to await tab.wait_request_loading(request_dict) for - loadingFinished (or sleep some secs), - and wait_loading=None will auto check response loaded.''' + loadingFinished (or sleep some secs) and wait_loading=None will auto check response loaded.''' request_id = self._ensure_request_id(request_dict) result = None if request_id is None: @@ -1648,13 +1647,13 @@ async def keyboard_send(self, timeout=timeout, **kwargs) - async def mouse_click_element(self, - cssselector: str, - button='left', - count=1, - scale=1, - multiplier=0.5, - timeout=NotSet): + async def mouse_click_element_rect(self, + cssselector: str, + button='left', + count=1, + scale=1, + multiplier=0.5, + timeout=NotSet): # dispatchMouseEvent on selected element center rect = await self.get_element_clip(cssselector, scale=scale, From 5b89c9f4aacbc5275c345ff2dfc2313ab47d2049 Mon Sep 17 00:00:00 2001 From: clericpy Date: Thu, 23 Jul 2020 00:03:38 +0800 Subject: [PATCH 6/7] multiplier of mouse_click_element_rect should be tuple --- ichrome/async_utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ichrome/async_utils.py b/ichrome/async_utils.py index a4f1b2a..0c5bfa5 100644 --- a/ichrome/async_utils.py +++ b/ichrome/async_utils.py @@ -1652,15 +1652,15 @@ async def mouse_click_element_rect(self, button='left', count=1, scale=1, - multiplier=0.5, + multiplier=(0.5, 0.5), timeout=NotSet): # dispatchMouseEvent on selected element center rect = await self.get_element_clip(cssselector, scale=scale, timeout=timeout) if rect: - x = rect['x'] + multiplier * rect['width'] - y = rect['y'] + multiplier * rect['height'] + x = rect['x'] + multiplier[0] * rect['width'] + y = rect['y'] + multiplier[1] * rect['height'] await self.mouse_press(x=x, y=y, button=button, From 8396295ce5a15ef0d266a317a8961798b1eb180f Mon Sep 17 00:00:00 2001 From: clericpy Date: Thu, 23 Jul 2020 00:05:57 +0800 Subject: [PATCH 7/7] 2.3.6 --- ichrome/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ichrome/__init__.py b/ichrome/__init__.py index a66dd85..f3fe030 100644 --- a/ichrome/__init__.py +++ b/ichrome/__init__.py @@ -6,7 +6,7 @@ from .logs import logger from .sync_utils import Chrome, Tab -__version__ = "2.3.5" +__version__ = "2.3.6" __tips__ = "[github]: https://github.com/ClericPy/ichrome\n[cdp]: https://chromedevtools.github.io/devtools-protocol/\n[cmd args]: https://peter.sh/experiments/chromium-command-line-switches/" __all__ = [ 'Chrome', 'ChromeDaemon', 'Tab', 'Tag', 'AsyncChrome', 'AsyncTab', 'logger',