diff --git a/DrissionPage/driver_element.py b/DrissionPage/driver_element.py index a297ad7..1c16104 100644 --- a/DrissionPage/driver_element.py +++ b/DrissionPage/driver_element.py @@ -213,7 +213,8 @@ class DriverElement(DrissionElement): if loc_or_str[0] == 'xpath': # 确保查询语句最前面是. - loc_str = f'.{loc_or_str[1]}' if not loc_or_str[1].startswith('.') else loc_or_str[1] + loc_str = loc_or_str[1] if loc_or_str[1].startswith(('.', '/')) else f'.//{loc_or_str[1]}' + loc_str = loc_str if loc_str.startswith('.') else f'.{loc_str}' loc_or_str = loc_or_str[0], loc_str else: if loc_or_str[1].lstrip().startswith('>'): diff --git a/DrissionPage/driver_page.py b/DrissionPage/driver_page.py index b9d2875..ee8b7f0 100644 --- a/DrissionPage/driver_page.py +++ b/DrissionPage/driver_page.py @@ -112,8 +112,11 @@ class DriverPage(object): """ if isinstance(loc_or_ele, str): loc_or_ele = get_loc_from_str(loc_or_ele) + if loc_or_ele[0] == 'xpath' and not loc_or_ele[1].startswith('/'): + loc_or_ele = 'xpath', f'//{loc_or_ele[1]}' elif isinstance(loc_or_ele, tuple) and len(loc_or_ele) == 2: - pass + if loc_or_ele[0] == 'xpath' and not loc_or_ele[1].startswith('/'): + loc_or_ele = 'xpath', f'//{loc_or_ele[1]}' elif isinstance(loc_or_ele, DriverElement): return loc_or_ele elif isinstance(loc_or_ele, WebElement): diff --git a/DrissionPage/session_element.py b/DrissionPage/session_element.py index e6fc25f..fc1ecdd 100644 --- a/DrissionPage/session_element.py +++ b/DrissionPage/session_element.py @@ -148,9 +148,10 @@ class SessionElement(DrissionElement): loc_str = None if loc_or_str[0] == 'xpath': - # Element的html是包含自己的,要如下处理,使其只检索下级的 - loc_str = loc_or_str[1] if loc_or_str[1].startswith('.') else f'.{loc_or_str[1]}' + loc_str = loc_or_str[1] if loc_or_str[1].startswith(('.', '/')) else f'.//{loc_or_str[1]}' + loc_str = loc_str if loc_str.startswith('.') else f'.{loc_str}' elif loc_or_str[0] == 'css selector': + # Element的html是包含自己的,要如下处理,使其只检索下级的 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 diff --git a/DrissionPage/session_page.py b/DrissionPage/session_page.py index 67eadb2..cc80246 100644 --- a/DrissionPage/session_page.py +++ b/DrissionPage/session_page.py @@ -99,8 +99,12 @@ class SessionPage(object): """ if isinstance(loc_or_ele, str): loc_or_ele = get_loc_from_str(loc_or_ele) + if loc_or_ele[0] == 'xpath' and not loc_or_ele[1].startswith('/'): + loc_or_ele = 'xpath', f'//{loc_or_ele[1]}' elif isinstance(loc_or_ele, tuple) and len(loc_or_ele) == 2: loc_or_ele = translate_loc_to_xpath(loc_or_ele) + if loc_or_ele[0] == 'xpath' and not loc_or_ele[1].startswith('/'): + loc_or_ele = 'xpath', f'//{loc_or_ele[1]}' elif isinstance(loc_or_ele, SessionElement): return loc_or_ele elif isinstance(loc_or_ele, Element):