-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
173 lines (149 loc) · 6.42 KB
/
client.py
File metadata and controls
173 lines (149 loc) · 6.42 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
import time
import logging
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
class NoSuchElementException(Exception):
pass
# Configure logging
logging.basicConfig(
filename="french_popular_bank_selenium.log",
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
class FrenchPopularBank:
def __init__(self, street: str = "Lyon", zip_code: str = "69000", position_on_map: int = 4):
"""
Initializes an instance of FrenchPopularBank.
:param street: The street for agency search (default is "Lyon").
:param zip_code: The postal code for agency search (default is "69000").
:param position_on_map: The position on the map to capture (default is 4).
"""
# Initialize the Selenium WebDriver (Ensure Selenium and the driver are installed)
self.driver = webdriver.Chrome()
self.street = street
self.zip_code = zip_code
self.position_on_map = position_on_map
def open_website(self):
"""
Opens the Banque Populaire web page.
"""
# Open the web page
self.driver.get("https://www.banquepopulaire.fr/")
# Wait for the page to fully load (you can adjust the wait time as needed)
self.driver.implicitly_wait(10)
def accept_cookies(self):
"""
Accepts cookies on the web page.
"""
# Locate the "Tout accepter" (Accept All) button by its CSS selector and click it
cookie_button = self.driver.find_element(By.CSS_SELECTOR, "#consent_prompt_submit")
time.sleep(1)
self.driver.save_screenshot("step_0.png")
cookie_button.click()
self.driver.save_screenshot("step_1.png")
logging.info("Accepted cookies")
def find_agency(self):
"""
Finds the "Trouver une agence" (Find an agency) section and clicks on it.
"""
# Locate the "Trouver une agence" (Find an agency) element by its <p> tag text
find_agency_element = self.driver.find_element(
By.XPATH, "//p[@class='font-text-body-bold' and contains(text(), 'Trouver une agence')]"
)
# Click on the element
find_agency_element.click()
self.driver.save_screenshot("step_2.png")
logging.info("Clicked on 'Trouver une agence'")
def fill_address(self):
"""
Fills in the address (Rue) and postal code fields in the agency search form.
"""
# Locate and fill in the postal code input field
postal_code_input = self.driver.find_element(By.ID, "em-search-form__searchcity")
postal_code_input.send_keys(self.zip_code)
# Locate and fill in the street (Rue) input field
street_input = self.driver.find_element(By.ID, "em-search-form__searchstreet")
street_input.send_keys(self.street)
# Simulate pressing Enter
street_input.send_keys(Keys.RETURN)
time.sleep(1)
self.driver.save_screenshot("step_3.png")
# Use JavaScript to click "Lyon Perrache" in the dropdown
js_click = f"document.getElementById('cgeocoder29_street_1').click();"
self.driver.execute_script(js_click)
self.driver.save_screenshot("step_4.png")
logging.info("Filled in address information")
def search_agency(self):
"""
Clicks the "Rechercher" (Search) button and waits for the redirection.
"""
# Click the "Rechercher" button
search_button = self.driver.find_element(By.CLASS_NAME, "em-search-form__submit")
search_button.click()
# Wait for the URL to change, indicating redirection
wait = WebDriverWait(self.driver, 10)
wait.until(EC.url_changes(self.driver.current_url))
time.sleep(1)
self.driver.save_screenshot("step_5.png")
logging.info("Clicked on 'Rechercher'")
def collect_agency_elements(self):
"""
Collects agency elements on the search results page.
"""
# Collect agency elements
self.driver.execute_script("window.scrollBy(0, 200);")
agency_elements = self.driver.find_elements(By.CLASS_NAME, "em-results__item")
return agency_elements
def capture_specific_agency(self):
"""
Captures a specific agency by its position on the map.
This function collects agency elements, hovers over the specified agency, and captures a screenshot.
:raises NoSuchElementException: If the specified agency is not found.
"""
agency_elements = self.collect_agency_elements()
if len(agency_elements) >= self.position_on_map:
element_to_hover = self.driver.find_element(
By.XPATH, f"//td[text()='{self.position_on_map}']"
)
self.hover_and_capture(element_to_hover, "step_6.png")
else:
raise NoSuchElementException("The specified agency was not found.")
def hover_and_capture(self, element, filename):
"""
Hovers over an element and captures a screenshot.
:param element: The element to hover over.
:param filename: The filename for the screenshot.
"""
# Move the mouse to the center of the element
action_chains = ActionChains(self.driver)
action_chains.move_to_element(element).perform()
time.sleep(1)
action_chains.move_by_offset(10, 0).perform()
action_chains.move_by_offset(0, 10).perform()
action_chains.move_by_offset(-10, 0).perform()
action_chains.move_by_offset(0, -10).perform()
time.sleep(1)
# Capture a screenshot before clicking the button
self.driver.save_screenshot(filename)
logging.info(f"Hovered over the element and captured a screenshot: {filename}")
def close_browser(self):
# Close the browser
self.driver.quit()
logging.info("Closed the browser")
if __name__ == "__main__":
bank = FrenchPopularBank()
try:
bank.open_website()
bank.accept_cookies()
bank.find_agency()
bank.fill_address()
bank.search_agency()
bank.capture_specific_agency()
except Exception as e:
logging.error(f"An error occurred: {str(e)}")
finally:
bank.close_browser()