Skip to content

Commit

Permalink
Merge pull request #35 from ClericPy/dev
Browse files Browse the repository at this point in the history
2.3.6
  • Loading branch information
ClericPy authored Jul 22, 2020
2 parents 2981b6d + 8396295 commit eb55bb5
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 11 deletions.
6 changes: 5 additions & 1 deletion examples_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion ichrome/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion ichrome/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
44 changes: 38 additions & 6 deletions ichrome/async_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -879,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:
Expand Down Expand Up @@ -1044,6 +1042,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:
Expand All @@ -1058,8 +1064,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,
Expand Down Expand Up @@ -1490,7 +1495,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,
Expand Down Expand Up @@ -1640,6 +1647,31 @@ async def keyboard_send(self,
timeout=timeout,
**kwargs)

async def mouse_click_element_rect(self,
cssselector: str,
button='left',
count=1,
scale=1,
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[0] * rect['width']
y = rect['y'] + multiplier[1] * 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,
Expand Down
4 changes: 2 additions & 2 deletions ichrome/debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit eb55bb5

Please sign in to comment.