From 79bb8d4500c24b018b4e8be9d5ba34fb47218f6f Mon Sep 17 00:00:00 2001 From: g1879 Date: Sun, 7 Jun 2020 00:09:46 +0800 Subject: [PATCH] =?UTF-8?q?get=5Floc=5Ffrom=5Fstr=E5=A2=9E=E5=8A=A0tag:tag?= =?UTF-8?q?Name@arg=3Dval=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/common.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/DrissionPage/common.py b/DrissionPage/common.py index 75e90c3..9e3f771 100644 --- a/DrissionPage/common.py +++ b/DrissionPage/common.py @@ -4,6 +4,7 @@ @Contact : g1879@qq.com @File : common.py """ +import re from abc import abstractmethod from pathlib import Path from typing import Union @@ -66,7 +67,12 @@ def get_loc_from_str(loc: str) -> tuple: by = loc_item[0] loc_by = 'xpath' if by == 'tag' and len(loc_item) == 2: - loc_str = f'//{loc_item[1]}' + if '@' not in loc_item[1]: + loc_str = f'//{loc_item[1]}' + else: + r = re.search(r'(.*?)@([^=]*)=?(.*)', loc_item[1]) + loc_str = f'//{r.group(1)}[@{r.group(2)}{"{}"}]' + loc_str = loc_str.format(f'="{r.group(3)}"') if r.group(3) else loc_str.format('') elif by.startswith('@') and len(loc_item) == 2: loc_str = f'//*[{by}="{loc_item[1]}"]' elif by.startswith('@') and len(loc_item) == 1: