-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyx_main.py
238 lines (202 loc) · 8.63 KB
/
pyx_main.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import os
import time
import pandas as pd
from dotenv import load_dotenv
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
load_dotenv()
DRIVER_PATH = os.getenv("DRIVER_PATH")
DATA_PATH = os.getenv("DATA_PATH")
USERNAME = os.getenv("PHY_USERNAME")
PASSWORD = os.getenv("PASSWORD")
def init_browser():
chrome_options = Options()
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-images")
chrome_options.add_argument("--disable-javascript")
service = Service(DRIVER_PATH)
driver = webdriver.Chrome(service=service, options=chrome_options)
return driver
def login(driver, data):
driver.get("https://phy-x.net/module/physics/shielding/")
register_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//button[text()='Register']"))
)
register_button.click()
print("Register button clicked")
login_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//button[text()='Login']"))
)
login_button.click()
print("Login button clicked")
time.sleep(2)
print("Time to login")
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, "user_mail"))
)
username_input = driver.find_element(By.XPATH, "//input[@label='Username']")
username_input.send_keys(USERNAME)
print("Username entered")
password_input = driver.find_element(By.XPATH, "//input[@label='Password']")
password_input.send_keys(PASSWORD)
print("Password entered")
authenticate_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//button[text()='Authenticate']"))
)
authenticate_button.click()
print("Authenticate button clicked")
print("Loading data\n", data.head(2))
time.sleep(5)
print("Page loaded, ready to fill data")
def enter_data(driver, data):
index = 0
while index < len(data):
row = data.iloc[index]
composition = row['Composition_Formula']
density = row['Theoretical_Density']
composition_id = f"formula{index}" if index > 0 else "formula"
density_id = f"density{index}" if index > 0 else "density"
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, composition_id))
).send_keys(composition)
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, density_id))
).send_keys(str(density))
index += 1
def initalizing_simulation(driver):
try:
analyze_button = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "button.primary.calculate"))
)
analyze_button.click()
print("Analyze button clicked")
except Exception as e:
print(f"An error occurred while clicking the Analyze button: {e}")
try:
ok_button= WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH,"//button[text()='OK']"))
)
ok_button.click()
print("OK button is clicked")
except Exception as e:
print(f"an error occurred while clicking OK button {e}")
def process_checkboxes(driver):
try:
all_checkboxes = WebDriverWait(driver, 20).until(
EC.presence_of_all_elements_located((By.XPATH, "//input[@type='checkbox' and @class='processor']"))
)
print(f"Found checkboxes: {len(all_checkboxes)}")
for checkbox in all_checkboxes:
if checkbox.is_selected():
driver.execute_script("arguments[0].click();", checkbox)
print("Checkbox deselected")
except Exception as e:
print(f"An error occurred while deselecting checkboxes: {e}")
print("All checkboxes processed.")
try:
all_checkboxes = WebDriverWait(driver, 20).until(
EC.presence_of_all_elements_located((By.XPATH, "//input[@type='checkbox' and @class='processor']"))
)
print(f"Found checkboxes: {len(all_checkboxes)}")
checkbox_ids_to_select = ['FNRCS']
for checkbox in all_checkboxes:
checkbox_id = checkbox.get_attribute('id')
if checkbox_id in checkbox_ids_to_select:
if not checkbox.is_selected():
driver.execute_script("arguments[0].click();", checkbox)
print(f"Checkbox with ID {checkbox_id} selected using JavaScript")
else:
print(f"Checkbox with ID {checkbox_id} was already selected")
else:
if checkbox.is_selected():
driver.execute_script("arguments[0].click();", checkbox)
print(f"Checkbox with ID {checkbox_id} deselected")
except Exception as e:
print(f"An error occurred while processing checkboxes: {e}")
print("Specific checkboxes processed.")
def accepting_terms(driver):
try:
accepted_checkbox = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.ID, "accepted"))
)
if not accepted_checkbox.is_selected():
driver.execute_script("arguments[0].click();", accepted_checkbox)
print("Checkbox 'accepted' selected using JavaScript")
else:
print("Checkbox 'accepted' was already selected")
except Exception as e:
print("Checkbox accepted was not selected", e)
try:
calculate_button = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH, "//div[contains(@class, 'ui buttons main double checkbox')]//button"))
)
driver.execute_script("arguments[0].scrollIntoView(true);", calculate_button)
WebDriverWait(driver, 20).until(
EC.element_to_be_clickable(calculate_button)
).click()
print("Calculate button clicked")
except Exception as e:
print(f"An error occurred while clicking the Calculate button: {e}")
print("Calculate button clicked")
def run_and_exprt_simulation(driver):
try:
performance_checkbox = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.ID, "performance-checkbox"))
)
if not performance_checkbox.is_selected():
driver.execute_script("arguments[0].click();", performance_checkbox)
print("Performance checkbox selected using JavaScript")
else:
print("Performance checkbox was already selected")
except Exception as e:
print(f"An error occurred while selecting the performance checkbox: {e}")
try:
ok_button_new_page = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//div[contains(@class, 'ui buttons main single foot')]//button"))
)
driver.execute_script("arguments[0].scrollIntoView(true);", ok_button_new_page)
start_time = time.time()
print("start_time", start_time)
ok_button_new_page.click()
print("OK button clicked on the new page")
except Exception as e:
print(f"An error occurred while clicking the OK button on the new page: {e}")
print("Performance checkbox and OK button on the new page processed.")
try:
export_button = WebDriverWait(driver, 600).until(
EC.presence_of_element_located((By.XPATH, "//button[text()='Export to Excel']"))
)
end_time = time.time()
print("end time", end_time)
export_button.click()
simulation_time = end_time - start_time
print("simulation time", simulation_time)
print(f"Simulation completed. Time taken: {simulation_time%60:.2f} seconds")
print("data has been exported")
except Exception as e:
print(f"An error occurred while waiting for the 'Export to Excel' button: {e}")
def main():
data = pd.read_csv(DATA_PATH)
data = data[1:6]
print(f"Processing {len(data)} rows of data.")
driver = init_browser()
try:
login(driver, data)
enter_data(driver, data)
time.sleep(3)
initalizing_simulation(driver)
process_checkboxes(driver)
accepting_terms(driver)
run_and_exprt_simulation(driver)
finally:
input('Press enter to close the browser')
driver.quit()
if __name__ == "__main__":
main()