优化逻辑

This commit is contained in:
g1879 2021-01-02 16:41:08 +08:00
parent c262a978f7
commit c0bfdfa1e9
4 changed files with 24 additions and 44 deletions

View File

@ -107,22 +107,16 @@ def str_to_loc(loc: str) -> tuple:
else: else:
loc = loc.replace('.', '@class=', 1) loc = loc.replace('.', '@class=', 1)
if loc.startswith('#'): elif loc.startswith('#'):
if loc.startswith(('#=', '#:',)): if loc.startswith(('#=', '#:',)):
loc = loc.replace('#', '@id', 1) loc = loc.replace('#', '@id', 1)
else: else:
loc = loc.replace('#', '@id=', 1) loc = loc.replace('#', '@id=', 1)
if loc.startswith(('x:', 'x=')): elif loc.startswith(('t:', 't=')):
loc = f'xpath:{loc[2:]}'
if loc.startswith(('c:', 'c=')):
loc = f'css:{loc[2:]}'
if loc.startswith(('t:', 't=')):
loc = f'tag:{loc[2:]}' loc = f'tag:{loc[2:]}'
if loc.startswith(('tx:', 'tx=')): elif loc.startswith(('tx:', 'tx=')):
loc = f'text{loc[2:]}' loc = f'text{loc[2:]}'
# 根据属性查找 # 根据属性查找
@ -135,7 +129,7 @@ def str_to_loc(loc: str) -> tuple:
loc_str = f'//*[@{loc[1:]}]' loc_str = f'//*[@{loc[1:]}]'
# 根据tag name查找 # 根据tag name查找
elif loc.startswith(('tag=', 'tag:')): elif loc.startswith(('tag:', 'tag=')):
if '@' not in loc[4:]: if '@' not in loc[4:]:
loc_str = f'//*[name()="{loc[4:]}"]' loc_str = f'//*[name()="{loc[4:]}"]'
else: else:
@ -149,7 +143,7 @@ def str_to_loc(loc: str) -> tuple:
loc_str = f'//*[name()="{at_lst[0]}" and @{r[0]}]' loc_str = f'//*[name()="{at_lst[0]}" and @{r[0]}]'
# 根据文本查找 # 根据文本查找
elif loc.startswith(('text=', 'text:')): elif loc.startswith(('text:', 'text=')):
if len(loc) > 5: if len(loc) > 5:
mode = 'exact' if loc[4] == '=' else 'fuzzy' mode = 'exact' if loc[4] == '=' else 'fuzzy'
loc_str = _make_xpath_str('*', 'text()', loc[5:], mode) loc_str = _make_xpath_str('*', 'text()', loc[5:], mode)
@ -157,13 +151,18 @@ def str_to_loc(loc: str) -> tuple:
loc_str = '//*[not(text())]' loc_str = '//*[not(text())]'
# 用xpath查找 # 用xpath查找
elif loc.startswith(('xpath=', 'xpath:')): elif loc.startswith(('xpath:', 'xpath=')):
loc_str = loc[6:] loc_str = loc[6:]
elif loc.startswith(('x:', 'x=')):
loc_str = loc[2:]
# 用css selector查找 # 用css selector查找
elif loc.startswith(('css=', 'css:')): elif loc.startswith(('css:', 'css=')):
loc_by = 'css selector' loc_by = 'css selector'
loc_str = loc[4:] loc_str = loc[4:]
elif loc.startswith(('c:', 'c=')):
loc_by = 'css selector'
loc_str = loc[2:]
# 根据文本模糊查找 # 根据文本模糊查找
else: else:

View File

@ -584,38 +584,21 @@ def execute_driver_find(page_or_ele,
page = page_or_ele page = page_or_ele
driver = page_or_ele.driver driver = page_or_ele.driver
try:
if timeout is not None and timeout != page.timeout: if timeout is not None and timeout != page.timeout:
wait = WebDriverWait(driver, timeout=timeout) wait = WebDriverWait(driver, timeout=timeout)
else: else:
page.wait._driver = driver page.wait._driver = driver
wait = page.wait 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获取
if mode == 'single':
if timeout:
return DriverElement(wait.until(ec.presence_of_element_located(loc)), page)
else:
try: try:
return DriverElement(driver.find_element_by_css_selector(loc[1]), page) if loc[0] == 'xpath':
except: return wait.until(ElementsByXpath(page, loc[1], mode, timeout))
return None else: # 使用css selector查找
if mode == 'single':
return DriverElement(wait.until(ec.presence_of_element_located(loc)), page)
elif mode == 'all': elif mode == 'all':
if timeout:
eles = wait.until(ec.presence_of_all_elements_located(loc)) eles = wait.until(ec.presence_of_all_elements_located(loc))
return [DriverElement(ele, page) for ele in eles] 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 []
except TimeoutException: except TimeoutException:
return [] if mode == 'all' else None return [] if mode == 'all' else None

View File

@ -199,7 +199,6 @@ class DriverPage(object):
else: else:
raise ValueError('Argument loc_or_str can only be tuple, str, DriverElement, DriverElement.') 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) return execute_driver_find(self, loc_or_ele, mode, timeout)
def eles(self, def eles(self,

View File

@ -74,7 +74,7 @@ class MixPage(Null, SessionPage, DriverPage):
loc_or_str: Union[Tuple[str, str], str, DriverElement, SessionElement, WebElement], loc_or_str: Union[Tuple[str, str], str, DriverElement, SessionElement, WebElement],
mode: str = 'single', mode: str = 'single',
timeout: float = None): timeout: float = None):
return self.ele(loc_or_str, mode, timeout or self.timeout) return self.ele(loc_or_str, mode, timeout)
@property @property
def url(self) -> Union[str, None]: def url(self) -> Union[str, None]:
@ -379,7 +379,6 @@ class MixPage(Null, SessionPage, DriverPage):
if self._mode == 's': if self._mode == 's':
return super().ele(loc_or_ele, mode=mode) return super().ele(loc_or_ele, mode=mode)
elif self._mode == 'd': 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) return super(SessionPage, self).ele(loc_or_ele, mode=mode, timeout=timeout)
def eles(self, def eles(self,