diff --git a/DrissionPage/_elements/chromium_element.py b/DrissionPage/_elements/chromium_element.py index f10d0bb..90bc03f 100644 --- a/DrissionPage/_elements/chromium_element.py +++ b/DrissionPage/_elements/chromium_element.py @@ -593,7 +593,27 @@ class ChromiumElement(DrissionElement): self._run_js('this.focus();') return self - def hover(self, offset_x=None, offset_y=None): + def keep_pseudo_state(self, forcedPseudoClasses): + while self.__dict__.get('keep_count', 0): + self.owner.run_cdp('CSS.enable') + self.owner.run_cdp('CSS.forcePseudoState', nodeId=self._node_id, forcedPseudoClasses=forcedPseudoClasses) + + def force_pseudo_state(self, keep_state=True, forcedPseudoClasses=None): + if not forcedPseudoClasses: + forcedPseudoClasses = ['hover'] + if keep_state and self.__dict__.get('keep_count', 0) == 0: + from random import randint + x, y = self.owner.rect.size + x, y = (x, y) if x <= y else (y, x) + self.owner.actions.move_to(self, offset_x=randint(x, y), offset_y=randint(x, y), duration=.1) + self.__dict__['keep_count'] = 1 + Thread(target=self.keep_pseudo_state, args=(forcedPseudoClasses,)).start() + if self.__dict__.get('keep_count', 0) > 0 and not keep_state: + del self.__dict__['keep_count'] + return self + + def hover(self, offset_x=None, offset_y=None, keep_hover=False): + self.force_pseudo_state(keep_hover, forcedPseudoClasses=['hover']) self.owner.actions.move_to(self, offset_x=offset_x, offset_y=offset_y, duration=.1) return self