-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_interaction.py
159 lines (126 loc) · 5.7 KB
/
web_interaction.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
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
import pickle
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from website_request import fetch_webpage_content
import time
import os
import bs4
def credentials(website: str, user_id: str, password_id: str,username: str, password: str) -> str:
#driver_path = r"C:\Users\lstalet\Documents\AutoGen\WSL - pentest\msedgedriver.exe"
driver_path = os.path.expanduser("~/drivers/edge/msedgedriver")
service = webdriver.edge.service.Service(driver_path)
options = webdriver.EdgeOptions()
driver = webdriver.Edge(service=service, options=options)
url = ""
page_source =""
try:
print(f"Navigating to {website}")
driver.get(website)
wait = WebDriverWait(driver, 20)
print(f"Waiting for the username field with id: {user_id}")
username_field = wait.until(EC.presence_of_element_located((By.NAME, user_id)))
print(f"Username field found: {username_field}")
username_field.clear()
username_field.send_keys(username)
print(f"Entered username: {username}")
print(f"Waiting for the password field with id: {password_id}")
password_field = driver.find_element(By.NAME, password_id)
print(f"Password field found: {password_field}")
password_field.clear()
password_field.send_keys(password)
print(f"Entered password")
password_field.send_keys(Keys.RETURN)
time.sleep(10)
save_cookies(driver)
page = driver.page_source
url = driver.current_url
print("The url of the accessed page is: ", url)
return page
except Exception as e:
print(f"Error during credentials input: {e}")
finally:
driver.quit()
return page
def click(button_id: str, website: str) -> str:
driver_path = os.path.expanduser("~/drivers/edge/msedgedriver")
#driver_path = r"C:\Users\lstalet\Documents\AutoGen\WSL - pentest\msedgedriver.exe"
service = webdriver.edge.service.Service(driver_path)
options = webdriver.EdgeOptions()
driver = webdriver.Edge(service=service, options=options)
url = ""
try:
print(f"Navigating to {website}")
driver.get(website)
wait = WebDriverWait(driver, 20)
print(f"Waiting for the button with xpath: //a[@href='{button_id}']")
button = wait.until(EC.element_to_be_clickable((By.XPATH, f"//a[@href='{button_id}']")))
print(f"Button found: {button}")
button.click()
print("Button clicked")
time.sleep(10)
page = driver.page_source
url = driver.current_url
print("The url of the accessed page is: ", url)
return page
except Exception as e:
print(f"Error during button click: {e}")
finally:
driver.quit()
return page
def fill(box_id: str, content: str, website: str) -> str:
driver_path = os.path.expanduser("~/drivers/edge/msedgedriver")
#driver_path = r"C:\Users\lstalet\Documents\AutoGen\WSL - pentest\msedgedriver.exe"
service = webdriver.edge.service.Service(driver_path)
options = webdriver.EdgeOptions()
driver = webdriver.Edge(service=service, options=options)
url = ""
page_source =""
try:
print(f"Navigating to {website}")
driver.get(website)
load_cookies(driver)
# Refresh the page to apply cookies
driver.refresh()
time.sleep(5)
wait = WebDriverWait(driver, 20)
print(f"Waiting for the input field with id: {box_id}")
username_field = wait.until(EC.presence_of_element_located((By.NAME, box_id)))
print(f"box field found: {box_id}")
username_field.clear()
username_field.send_keys(content)
print(f"Entered content: {content}")
username_field.send_keys(Keys.RETURN)
time.sleep(10)
page = driver.page_source
url = driver.current_url
print("The url of the accessed page is: ", url)
except Exception as e:
print(f"Error during content input: {e}")
finally:
driver.quit()
return page
def save_cookies(driver):
#with open(r"C:\Users\lstalet\Documents\AutoGen\WSL - pentest\groupchat\Outputs\cookies.pk1", 'wb') as file:
with open("/mnt/c/Users/lstalet/documents/autogen/wsl - pentest/groupchat/Outputs/cookies.pk1", 'wb') as file:
pickle.dump(driver.get_cookies(), file)
print(f"Cookies saved to /mnt/c/Users/lstalet/Documents/autogen/WSL - pentest/groupchat/Outputs")
def load_cookies(driver):
#with open(r"C:\Users\lstalet\Documents\AutoGen\WSL - pentest\groupchat\Outputs\cookies.pk1", 'rb') as file:
with open("/mnt/c/Users/lstalet/Documents/autogen/WSL - pentest/groupchat/Outputs/cookies.pk1", 'rb') as file:
cookies = pickle.load(file)
for cookie in cookies:
driver.add_cookie(cookie)
print(f"Cookies loaded from /mnt/c/Users/lstalet/Documents/autogen/WSL - pentest/groupchat/Outputs")
def web_filter(url: str) -> str:
content = fetch_webpage_content(url)
soup = bs4.BeautifulSoup(content, 'html.parser')
links = soup.find_all('a')
input_spaces = soup.find_all('input')
filtered_elements = {
'links': [link.get('href') for link in links if link.get('href')],
'inputs': [str(input_space) for input_space in input_spaces]
}
return( filtered_elements)