-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrawler.py
More file actions
197 lines (159 loc) · 6.96 KB
/
Crawler.py
File metadata and controls
197 lines (159 loc) · 6.96 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# Python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup
import time
def login(username, password, elevance):
chrome_options = Options()
# Uncomment to run in headless mode if needed:
# chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
standard_login_url = "https://www.mobilehealthconsumer.com/weba/#/auth/login"
elevance_login_url = "https://angulartest.mobilehealthconsumer.com/"
if not elevance:
login_url = standard_login_url
driver.get(login_url)
wait = WebDriverWait(driver, 20)
username_field = wait.until(EC.visibility_of_element_located((By.ID, "username")))
password_field = driver.find_element(By.ID, "password")
username_field.send_keys(username)
password_field.send_keys(password)
login_button = driver.find_element(By.XPATH, "//button[contains(.,'Log in')]")
login_button.click()
# Wait until redirect from login page
wait.until(lambda d: "auth/login" not in d.current_url)
time.sleep(20) # Extend wait if Angular takes longer
driver.execute_script("""
var callback = arguments[arguments.length - 1];
if (window.getAllAngularTestabilities) {
Promise.all(window.getAllAngularTestabilities().map(function(testability) {
return new Promise(function(resolve) {
testability.whenStable(resolve);
});
})).then(callback);
} else {
callback();
}
""")
driver.fullscreen_window()
time.sleep(5) # Additional wait for Angular to stabilize
print("Logged in successfully")
print("---------------" * 5)
return driver
else:
login_url = elevance_login_url
driver.get(login_url)
wait = WebDriverWait(driver, 20)
driver = access_shadow_root(driver) # Access shadow root elements
time.sleep(10)
username_field = wait.until(EC.visibility_of_element_located((By.ID, "userName")))
password_field = driver.find_element(By.ID, "passWord")
hostname_field = driver.find_element(By.ID, "hostName")
username_field.send_keys(username)
password_field.send_keys(password)
hostname_field.clear()
hostname_field.send_keys("www.mobilehealthconsumer.com")
login_button = driver.find_element(By.XPATH, "//button[contains(.,'Login')]")
login_button.click()
# Wait until redirect from login page
wait.until(lambda d: "auth/login" not in d.current_url)
time.sleep(20) # Extend wait if Angular takes longer
driver.execute_script("""
var callback = arguments[arguments.length - 1];
if (window.getAllAngularTestabilities) {
Promise.all(window.getAllAngularTestabilities().map(function(testability) {
return new Promise(function(resolve) {
testability.whenStable(resolve);
});
})).then(callback);
} else {
callback();
}
""")
driver.fullscreen_window()
time.sleep(5) # Additional wait for Angular to stabilize
print("Logged in successfully")
print("---------------" * 5)
return driver
def access_shadow_root(driver):
for el in driver.find_elements(By.CSS_SELECTOR, "app-html-component"):
if el.is_displayed():
try:
# Get the shadow root content
shadow_content = driver.execute_script("""
return arguments[0].shadowRoot.innerHTML;
""", el)
# Replace the original element with shadow content
driver.execute_script("""
arguments[0].innerHTML = arguments[1];
arguments[0].shadowRoot.remove();
""", el, shadow_content)
except Exception:
pass
return driver
def get_html(driver):
try:
# Get page source with proper encoding
html = driver.page_source.encode('utf-8', errors='ignore').decode('utf-8')
soup = BeautifulSoup(html, "html.parser", from_encoding='utf-8')
hidden_contents = []
num = 0
for el in driver.find_elements(By.CSS_SELECTOR, "app-html-component"):
if el.is_displayed():
shadow_content = driver.execute_script("""
return arguments[0].shadowRoot.innerHTML;
""", el)
# Handle shadow content encoding
if shadow_content:
shadow_content = shadow_content.encode('utf-8', errors='ignore').decode('utf-8')
hidden_contents.append((num, shadow_content))
num += 1
shadow_breakpoints = soup.find_all('app-html-component')
for hidden_content in hidden_contents:
hidden_soup = BeautifulSoup(hidden_content[1], "html.parser", from_encoding='utf-8')
hidden_breakpoint = shadow_breakpoints[hidden_content[0]]
hidden_breakpoint.append(hidden_soup)
return soup
except Exception as e:
print("An error occurred in get_html:", e)
return None
def crawl(html, elavence):
links = []
try:
links = []
for link in html.find_all('a'):
links.append(str(link.get('href')))
except Exception as e:
print("An error occurred in crawl in links:", e)
if elavence:
# Filter links for elevance
new_links = []
for link in links:
if "dashboard" in link and "angulartest" not in link:
new_links.append("https://angulartest.mobilehealthconsumer.com" + link)
return new_links
return links
def get_page(driver, link=None):
if link is None:
time.sleep(5)
driver.fullscreen_window()
time.sleep(10) # Additional wait for Angular to stabilize
driver = access_shadow_root(driver) # Access shadow root elements
time.sleep(10) # Additional wait for Angular to stabilize
else:
try:
driver.get(link)
time.sleep(15)
driver.fullscreen_window()
time.sleep(10) # Additional wait for Angular to stabilize
driver = access_shadow_root(driver) # Access shadow root elements
time.sleep(10) # Additional wait for Angular to stabilize
except Exception as e:
print(f"An error occurred while crawling {link}:", e)
return driver