From 8d1d3b554dea44b3342c82199008a9c5241bbd5c Mon Sep 17 00:00:00 2001 From: g1879 Date: Wed, 9 Oct 2024 14:45:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96css=5Fpath?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_elements/chromium_element.py | 4 ++-- DrissionPage/_elements/session_element.py | 17 +++++++++-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/DrissionPage/_elements/chromium_element.py b/DrissionPage/_elements/chromium_element.py index 9424e2f..7ddfdcf 100644 --- a/DrissionPage/_elements/chromium_element.py +++ b/DrissionPage/_elements/chromium_element.py @@ -647,7 +647,8 @@ class ChromiumElement(DrissionElement): txt1 = ''' let i = el.getAttribute("id"); if (i){path = '>' + el.tagName.toLowerCase() + "#" + i + path; - break;} + el = el.parentNode; + continue;} ''' txt3 = '' txt4 = '''path = '>' + el.tagName.toLowerCase() + ":nth-child(" + nth + ")" + path;''' @@ -655,7 +656,6 @@ class ChromiumElement(DrissionElement): js = '''function(){ function e(el) { - //return el; if (!(el instanceof Element)) return; let path = ''; while (el.nodeType === Node.ELEMENT_NODE) { diff --git a/DrissionPage/_elements/session_element.py b/DrissionPage/_elements/session_element.py index 914f7dc..169fe5b 100644 --- a/DrissionPage/_elements/session_element.py +++ b/DrissionPage/_elements/session_element.py @@ -149,19 +149,20 @@ class SessionElement(DrissionElement): path_str = '' ele = self - while ele: - if xpath: + if xpath: + while ele: brothers = len(ele.eles(f'xpath:./preceding-sibling::{ele.tag}')) path_str = f'/{ele.tag}[{brothers + 1}]{path_str}' - else: + ele = ele.parent() + + else: + while ele: id_ = ele.attr('id') 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}' - - ele = ele.parent() + else: + path_str = f'>{ele.tag}:nth-child({len(ele.eles("xpath:./preceding-sibling::*")) + 1}){path_str}' + ele = ele.parent() return path_str if xpath else f'{path_str[1:]}'