Repository navigation
Expand file tree
/
Copy pathdynamic_selectors.py
More file actions
48 lines (40 loc) · 1.64 KB
/
Copy pathdynamic_selectors.py
File metadata and controls
48 lines (40 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from bs4 import BeautifulSoup
def auto_detect_selectors(html):
soup = BeautifulSoup(html, "html.parser")
# Step 1: Define important attributes dynamically
important_attributes = ["class", "id", "name"]
def build_selector(tag):
selector = tag.name
for attr in tag.attrs:
if attr in important_attributes or attr.startswith("data-"):
value = tag.attrs[attr]
if attr == "class":
selector += "." + ".".join(value)
elif attr == "id":
selector += f"#{value}"
else:
selector += f'[{attr}="{value}"]'
return selector
# Step 2: Generate selectors
css_selectors = []
xpath_selectors = []
for tag in soup.find_all(True):
css_selectors.append(build_selector(tag))
# XPath generation
xpath_parts = []
current = tag
while current is not None and current.name is not None:
part = current.name
for attr in tag.attrs:
if attr in important_attributes or attr.startswith("data-"):
value = tag.attrs[attr]
if attr == "class":
part += f'[contains(@class, "{value}")]'
elif attr == "id":
part += f'[@id="{value}"]'
else:
part += f'[@{attr}="{value}"]'
xpath_parts.insert(0, part)
current = current.parent
xpath_selectors.append("/" + "/".join(xpath_parts))
return {"css_selectors": css_selectors, "xpath_selectors": xpath_selectors}