update DrissionPage/_elements/chromium_element.py.

新增强制保持元素状态

Signed-off-by: Wen-Ming21 <3348431908@qq.com>
This commit is contained in:
Wen-Ming21 2024-10-02 06:54:45 +00:00 committed by Gitee
parent cde7fc7b8d
commit 1bb32ba617
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

View File

@ -593,7 +593,27 @@ class ChromiumElement(DrissionElement):
self._run_js('this.focus();') self._run_js('this.focus();')
return self 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) self.owner.actions.move_to(self, offset_x=offset_x, offset_y=offset_y, duration=.1)
return self return self