-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
98 lines (86 loc) · 3.28 KB
/
__init__.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import regex
class Iframes:
iframes = {}
alltagsonpage = {}
def __init__(
self,
driver,
By,
WebDriverWait,
expected_conditions,
seperator_for_duplicated_iframe="Ç",
ignore_google_ads=True,
):
self.By = By
self.WebDriverWait = WebDriverWait
self.expected_conditions = expected_conditions
self.driver = driver
self.seperator_for_duplicated_iframe = seperator_for_duplicated_iframe
self.iframes = {}
self.driver.switch_to.default_content()
self.ignore_google_ads = ignore_google_ads
self.iframes["mainframe"] = ""
self.mainframegemacht = True
self.__map__([])
self.driver.switch_to.default_content()
def __map__(self, path):
iframesn = self.driver.find_elements(self.By.TAG_NAME, "iframe")
iframesn = [("iframe", x) for x in iframesn]
iframes2 = self.driver.find_elements(self.By.TAG_NAME, "frame")
iframes2 = [("frame", x) for x in iframes2]
iframesn.extend(iframes2)
allatributes = []
for tagname, iframe in iframesn:
try:
for attr in iframe.get_property("attributes"):
allat = rf"""[{attr['name']}="{attr['value']}"]"""
allatributes.append(allat)
except Exception as Fehler:
print(Fehler)
try:
key = tagname + "".join(allatributes)
if self.ignore_google_ads:
if "google_ads_iframe" in key:
continue
tempnummer = 0
tempkey = key
while key in self.iframes:
key = (
tempkey
+ self.seperator_for_duplicated_iframe
+ str(tempnummer).zfill(6)
)
tempnummer = tempnummer + 1
if not self.mainframegemacht:
self.iframes["mainframe"] = path + [iframe]
self.mainframegemacht = True
elif self.mainframegemacht:
self.iframes[key] = path + [iframe]
self.driver.switch_to.frame(iframe)
self.__map__(self.iframes[key])
except Exception as Fehler:
print(Fehler)
self.driver.switch_to.parent_frame()
def switch_to(self, key):
self.driver.switch_to.default_content()
if key == "mainframe":
self.driver.switch_to.default_content()
if key not in self.iframes:
self.__map__([])
if key not in self.iframes:
try:
wait = self.WebDriverWait(self.driver, 20)
regex.sub(rf"{self.seperator_for_duplicated_iframe}\d{{6}}$", "", key)
wait.until(
self.expected_conditions.frame_to_be_available_and_switch_to_it(
(self.By.CSS_SELECTOR, key)
)
)
except Exception as Fehler:
print(Fehler)
else:
for iframe in self.iframes[key]:
try:
self.driver.switch_to.frame(iframe)
except Exception as Fehler:
print(Fehler)