mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
优化逻辑
This commit is contained in:
parent
c262a978f7
commit
c0bfdfa1e9
@ -107,22 +107,16 @@ def str_to_loc(loc: str) -> tuple:
|
||||
else:
|
||||
loc = loc.replace('.', '@class=', 1)
|
||||
|
||||
if loc.startswith('#'):
|
||||
elif loc.startswith('#'):
|
||||
if loc.startswith(('#=', '#:',)):
|
||||
loc = loc.replace('#', '@id', 1)
|
||||
else:
|
||||
loc = loc.replace('#', '@id=', 1)
|
||||
|
||||
if loc.startswith(('x:', 'x=')):
|
||||
loc = f'xpath:{loc[2:]}'
|
||||
|
||||
if loc.startswith(('c:', 'c=')):
|
||||
loc = f'css:{loc[2:]}'
|
||||
|
||||
if loc.startswith(('t:', 't=')):
|
||||
elif loc.startswith(('t:', 't=')):
|
||||
loc = f'tag:{loc[2:]}'
|
||||
|
||||
if loc.startswith(('tx:', 'tx=')):
|
||||
elif loc.startswith(('tx:', 'tx=')):
|
||||
loc = f'text{loc[2:]}'
|
||||
|
||||
# 根据属性查找
|
||||
@ -135,7 +129,7 @@ def str_to_loc(loc: str) -> tuple:
|
||||
loc_str = f'//*[@{loc[1:]}]'
|
||||
|
||||
# 根据tag name查找
|
||||
elif loc.startswith(('tag=', 'tag:')):
|
||||
elif loc.startswith(('tag:', 'tag=')):
|
||||
if '@' not in loc[4:]:
|
||||
loc_str = f'//*[name()="{loc[4:]}"]'
|
||||
else:
|
||||
@ -149,7 +143,7 @@ def str_to_loc(loc: str) -> tuple:
|
||||
loc_str = f'//*[name()="{at_lst[0]}" and @{r[0]}]'
|
||||
|
||||
# 根据文本查找
|
||||
elif loc.startswith(('text=', 'text:')):
|
||||
elif loc.startswith(('text:', 'text=')):
|
||||
if len(loc) > 5:
|
||||
mode = 'exact' if loc[4] == '=' else 'fuzzy'
|
||||
loc_str = _make_xpath_str('*', 'text()', loc[5:], mode)
|
||||
@ -157,13 +151,18 @@ def str_to_loc(loc: str) -> tuple:
|
||||
loc_str = '//*[not(text())]'
|
||||
|
||||
# 用xpath查找
|
||||
elif loc.startswith(('xpath=', 'xpath:')):
|
||||
elif loc.startswith(('xpath:', 'xpath=')):
|
||||
loc_str = loc[6:]
|
||||
elif loc.startswith(('x:', 'x=')):
|
||||
loc_str = loc[2:]
|
||||
|
||||
# 用css selector查找
|
||||
elif loc.startswith(('css=', 'css:')):
|
||||
elif loc.startswith(('css:', 'css=')):
|
||||
loc_by = 'css selector'
|
||||
loc_str = loc[4:]
|
||||
elif loc.startswith(('c:', 'c=')):
|
||||
loc_by = 'css selector'
|
||||
loc_str = loc[2:]
|
||||
|
||||
# 根据文本模糊查找
|
||||
else:
|
||||
|
@ -584,38 +584,21 @@ def execute_driver_find(page_or_ele,
|
||||
page = page_or_ele
|
||||
driver = page_or_ele.driver
|
||||
|
||||
if timeout is not None and timeout != page.timeout:
|
||||
wait = WebDriverWait(driver, timeout=timeout)
|
||||
else:
|
||||
page.wait._driver = driver
|
||||
wait = page.wait
|
||||
|
||||
try:
|
||||
if timeout is not None and timeout != page.timeout:
|
||||
wait = WebDriverWait(driver, timeout=timeout)
|
||||
else:
|
||||
page.wait._driver = driver
|
||||
wait = page.wait
|
||||
|
||||
if loc[0] == 'xpath':
|
||||
if timeout:
|
||||
return wait.until(ElementsByXpath(page, loc[1], mode, timeout))
|
||||
else:
|
||||
return ElementsByXpath(page, loc[1], mode, timeout)(driver)
|
||||
else: # 用css获取
|
||||
return wait.until(ElementsByXpath(page, loc[1], mode, timeout))
|
||||
else: # 使用css selector查找
|
||||
if mode == 'single':
|
||||
if timeout:
|
||||
return DriverElement(wait.until(ec.presence_of_element_located(loc)), page)
|
||||
else:
|
||||
try:
|
||||
return DriverElement(driver.find_element_by_css_selector(loc[1]), page)
|
||||
except:
|
||||
return None
|
||||
|
||||
return DriverElement(wait.until(ec.presence_of_element_located(loc)), page)
|
||||
elif mode == 'all':
|
||||
if timeout:
|
||||
eles = wait.until(ec.presence_of_all_elements_located(loc))
|
||||
return [DriverElement(ele, page) for ele in eles]
|
||||
else:
|
||||
try:
|
||||
eles = driver.find_elements_by_css_selector(loc[1])
|
||||
return [DriverElement(ele, page) for ele in eles]
|
||||
except:
|
||||
return []
|
||||
eles = wait.until(ec.presence_of_all_elements_located(loc))
|
||||
return [DriverElement(ele, page) for ele in eles]
|
||||
|
||||
except TimeoutException:
|
||||
return [] if mode == 'all' else None
|
||||
|
@ -199,7 +199,6 @@ class DriverPage(object):
|
||||
else:
|
||||
raise ValueError('Argument loc_or_str can only be tuple, str, DriverElement, DriverElement.')
|
||||
|
||||
timeout = timeout if timeout is not None else self.timeout
|
||||
return execute_driver_find(self, loc_or_ele, mode, timeout)
|
||||
|
||||
def eles(self,
|
||||
|
@ -74,7 +74,7 @@ class MixPage(Null, SessionPage, DriverPage):
|
||||
loc_or_str: Union[Tuple[str, str], str, DriverElement, SessionElement, WebElement],
|
||||
mode: str = 'single',
|
||||
timeout: float = None):
|
||||
return self.ele(loc_or_str, mode, timeout or self.timeout)
|
||||
return self.ele(loc_or_str, mode, timeout)
|
||||
|
||||
@property
|
||||
def url(self) -> Union[str, None]:
|
||||
@ -379,7 +379,6 @@ class MixPage(Null, SessionPage, DriverPage):
|
||||
if self._mode == 's':
|
||||
return super().ele(loc_or_ele, mode=mode)
|
||||
elif self._mode == 'd':
|
||||
timeout = timeout if timeout is not None else self.timeout
|
||||
return super(SessionPage, self).ele(loc_or_ele, mode=mode, timeout=timeout)
|
||||
|
||||
def eles(self,
|
||||
|
Loading…
x
Reference in New Issue
Block a user