diff --git a/DrissionPage/_base/base.py b/DrissionPage/_base/base.py index b0e69ca..2dd52e4 100644 --- a/DrissionPage/_base/base.py +++ b/DrissionPage/_base/base.py @@ -121,11 +121,8 @@ class DrissionElement(BaseElement): :param text_node_only: 是否只返回文本节点 :return: 文本列表 """ - if text_node_only: - texts = self.eles('xpath:/text()') - else: - texts = [x if isinstance(x, str) else x.text for x in self.eles('xpath:./text() | *')] - + texts = self.eles('xpath:/text()') if text_node_only else [x if isinstance(x, str) else x.text + for x in self.eles('xpath:./text() | *')] return [format_html(x.strip(' ').rstrip('\n')) for x in texts if x and sub('[\r\n\t ]', '', x) != ''] def parent(self, level_or_loc=1, index=1): @@ -139,10 +136,8 @@ class DrissionElement(BaseElement): elif isinstance(level_or_loc, (tuple, str)): loc = get_loc(level_or_loc, True) - if loc[0] == 'css selector': raise ValueError('此css selector语法不受支持,请换成xpath。') - loc = f'xpath:./ancestor::{loc[1].lstrip(". / ")}[{index}]' else: diff --git a/DrissionPage/_functions/web.py b/DrissionPage/_functions/web.py index c4c6242..a53ae98 100644 --- a/DrissionPage/_functions/web.py +++ b/DrissionPage/_functions/web.py @@ -56,7 +56,7 @@ def get_ele_txt(e): if sub('[ \n\t\r]', '', el) != '': # 字符除了回车和空格还有其它内容 txt = el if not pre: - txt = txt.replace('\r\n', ' ').replace('\n', ' ').strip(' ') + txt = txt.replace('\r\n', ' ').replace('\n', ' ') txt = sub(r' {2,}', ' ', txt) str_list.append(txt) @@ -77,8 +77,26 @@ def get_ele_txt(e): re_str = get_node_txt(e) if re_str and re_str[-1] == '\n': re_str.pop() - re_str = ''.join([i if i is not True else '\n' for i in re_str]) - return format_html(re_str) + + re_str = [i if i is not True else '\n' for i in re_str] + l = len(re_str) + if l > 1: + r = [] + for i in range(0, l - 1, 2): + i1 = re_str[i] + i2 = re_str[i + 1] + if i1.endswith(' ') and i2.startswith(' '): + i1 = i1[:-1] + r.append(i1) + r.append(i2) + re_str = ''.join(r) + + elif not l: + re_str = '' + else: + re_str = re_str[0] + + return format_html(re_str.strip()) def format_html(text): diff --git a/DrissionPage/_pages/chromium_base.py b/DrissionPage/_pages/chromium_base.py index 98107c5..925df69 100644 --- a/DrissionPage/_pages/chromium_base.py +++ b/DrissionPage/_pages/chromium_base.py @@ -547,6 +547,8 @@ class ChromiumBase(BasePage): :param index: 获取第几个,从1开始,可传入负数获取倒数第几个 :return: SessionElement对象或属性、文本 """ + if locator: + self.wait.eles_loaded(locator) return make_session_ele(self, locator, index=index, method='s_ele()') def s_eles(self, locator): @@ -554,6 +556,7 @@ class ChromiumBase(BasePage): :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :return: SessionElement对象组成的列表 """ + self.wait.eles_loaded(locator) return make_session_ele(self, locator, index=None) def _find_elements(self, locator, timeout=None, index=1, relative=False, raise_err=None):