From cde7fc7b8d5e9cd7f8918b506e56af53489ce9a7 Mon Sep 17 00:00:00 2001 From: Wen-Ming21 <3348431908@qq.com> Date: Wed, 25 Sep 2024 14:42:21 +0000 Subject: [PATCH] update DrissionPage/_elements/session_element.py. Signed-off-by: Wen-Ming21 <3348431908@qq.com> --- DrissionPage/_elements/session_element.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/DrissionPage/_elements/session_element.py b/DrissionPage/_elements/session_element.py index d0cba7a..4a9aa86 100644 --- a/DrissionPage/_elements/session_element.py +++ b/DrissionPage/_elements/session_element.py @@ -155,11 +155,14 @@ class SessionElement(DrissionElement): if id_: path_str = f'>{ele.tag}#{id_}{path_str}' break - brothers = len(ele.eles(f'xpath:./preceding-sibling::*')) - path_str = f'>{ele.tag}:nth-child({brothers + 1}){path_str}' + before_brothers = len(ele.eles(f'xpath:./preceding-sibling::*')) + after_brothers = len(ele.eles(f'xpath:./following-sibling::*')) + path_str = f'>{ele.tag}:nth-child({before_brothers + 1}){path_str}' if ( + before_brothers or after_brothers) else f'>{ele.tag}{path_str}' else: - brothers = len(ele.eles(f'xpath:./preceding-sibling::{ele.tag}')) - path_str = f'/{ele.tag}[{brothers + 1}]{path_str}' if brothers > 0 else f'/{ele.tag}{path_str}' + before_brothers = len(ele.eles(f'xpath:./preceding-sibling::{ele.tag}')) + after_brothers = len(ele.eles(f'xpath:./following-sibling::{ele.tag}')) + path_str = f'/{ele.tag}[{before_brothers + 1}]{path_str}' if before_brothers > 0 else f'/{ele.tag}[1]{path_str}' if after_brothers > 0 else f'/{ele.tag}{path_str}' ele = ele.parent()