针对css方式的小优化

This commit is contained in:
g1879 2020-08-11 17:41:53 +08:00
parent a522b472e0
commit 16755abad6
2 changed files with 9 additions and 9 deletions

View File

@ -163,6 +163,9 @@ class DriverElement(DrissionElement):
# 确保查询语句最前面是.
loc_str = f'.{loc_or_str[1]}' if not loc_or_str[1].startswith('.') else loc_or_str[1]
loc_or_str = loc_or_str[0], loc_str
else:
if loc_or_str[1].lstrip().startswith('>'):
raise ValueError('WebElement does not support getting direct child elements.')
timeout = timeout or self.timeout
return execute_driver_find(self.inner_ele, loc_or_str, mode, show_errmsg, timeout)
@ -405,19 +408,15 @@ def execute_driver_find(page_or_ele: Union[WebElement, WebDriver],
mode = mode or 'single'
if mode not in ['single', 'all']:
raise ValueError("Argument mode can only be 'single' or 'all'.")
msg = result = None
try:
wait = WebDriverWait(page_or_ele, timeout=timeout)
if mode == 'single':
msg = 'Element not found.'
result = DriverElement(wait.until(ec.presence_of_element_located(loc)))
return DriverElement(wait.until(ec.presence_of_element_located(loc)))
elif mode == 'all':
msg = 'Elements not found.'
eles = wait.until(ec.presence_of_all_elements_located(loc))
result = [DriverElement(ele) for ele in eles]
return result
return [DriverElement(ele) for ele in eles]
except:
if show_errmsg:
print(msg, loc)
print('Element(s) not found.', loc)
raise
return [] if mode == 'all' else None

View File

@ -123,7 +123,8 @@ class SessionElement(DrissionElement):
# Element的html是包含自己的要如下处理使其只检索下级的
loc_str = loc_or_str[1] if loc_or_str[1].startswith('.') else f'.{loc_or_str[1]}'
elif loc_or_str[0] == 'css selector':
loc_str = f':root>{self.tag}{loc_or_str[1]}'
loc_str = loc_or_str[1] if loc_or_str[1][0] in '>, ' else f' {loc_or_str[1]}'
loc_str = f':root>{self.tag}{loc_str}'
loc_or_str = loc_or_str[0], loc_str
return execute_session_find(self.inner_ele, loc_or_str, mode, show_errmsg)
@ -217,7 +218,7 @@ def execute_session_find(page_or_ele: BaseParser,
elif 'HtmlElement' in str(type(page_or_ele.element)): # 从元素查找
elements = page_or_ele.element.xpath(loc_str)
ele = [Element(element=e, url=page_or_ele.url) for e in elements]
else:
else: # 用css selector获取
ele = page_or_ele.find(loc_str)
if mode == 'single':