def move( self, selector: Union[str, ElementHandle], padding_percentage: Optional[float] = None, wait_for_selector: Optional[float] = None, ) -> None: self.toggle_random_move(False) elem = None if isinstance(selector, str): if wait_for_selector: self.page.wait_for_selector(selector, timeout=wait_for_selector) elem = self.page.query_selector(selector) if elem is None: raise Exception( 'Could not find element with selector "${}", make sure you\'re waiting for the elements with "puppeteer.wait_for_selector"'.format( selector ) ) else: # ElementHandle elem = selector # Make sure the object is in view elem.scroll_into_view_if_needed() box = elem.bounding_box() if box is None: raise Exception( "Could not find the dimensions of the element you're clicking on, this might be a bug?" ) destination = get_random_box_point(box, padding_percentage) dimensions = {"height": box["height"], "width": box["width"]} overshooting = should_overshoot(self.previous, destination) to = ( overshoot(destination, self.overshoot_radius) if overshooting else destination ) self.trace_path(path(self.previous, to)) if overshooting: bounding_box = { "height": dimensions["height"], "width": dimensions["width"], "x": destination.x, "y": destination.y, } correction = path(to, bounding_box, self.overshoot_spread) self.trace_path(correction) self.previous = destination self.toggle_random_move(True)
async def random_move(self): """Start random mouse movements. Function recursively calls itself""" try: if not self.moving: rand = await self.get_random_page_point() await self.trace_path(path(self.previous, rand), True) self.previous = rand await asyncio.sleep(random.random() * 2) asyncio.ensure_future( self.random_move()) # fire and forget, recursive function except: logger.debug("Warning: stopping random mouse movements")
def move_to(self, destination: dict) -> None: destination_vector = Vector(destination["x"], destination["y"]) self.toggle_random_move(False) self.trace_path(path(self.previous, destination_vector)) self.toggle_random_move(True)