from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait, Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
import time
# Set up Chrome options
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "")
# Specify the path to the ChromeDriver executable
driver_path = r'
service = Service(driver_path)
# Initialize the WebDriver with the configured options
driver = webdriver.Chrome(service=service, options=chrome_options)
def check_link_open(expected_url, driver):
while driver.current_url != expected_url:
print(f"Waiting for the link to open... Current URL:
{driver.current_url}")
time.sleep(2) # Check every 2 seconds
print(f"Expected link opened: {driver.current_url}")
try:
# Open the initial URL
initial_url =
"https://www.blsspainmorocco.net/MAR/Appointment/VisaType"
driver.get(initial_url)
# Wait until the page is fully loaded
WebDriverWait(driver, 20).until(
lambda d: d.execute_script("return document.readyState") ==
"complete"
)
print("Initial page loaded successfully.")
# Ensure page URL is correct
current_url = driver.current_url
print(f"URL changed to {current_url}")
# Wait and click on 'Family' appointment type
try:
appointment_Family = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH,
"//label[text()='Appointment For
*']/following-sibling::div//label[contains(text(), 'Family')]"))
)
appointment_Family.click()
print("Selected 'Family'")
except Exception as e:
print(f"Error selecting 'Family': {e}")
driver.save_screenshot("error_Family.png") # Debug screenshot
# Wait and select the Location (e.g., Tangier)
try:
location_dropdown = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH,
"//label[text()='Location *']/following-sibling::span//select"))
)
location_select = Select(location_dropdown)
location_select.select_by_visible_text("Tangier")
print("Selected location 'Tangier'")
except Exception as e:
print(f"Error selecting location: {e}")
driver.save_screenshot("error_location.png") # Debug
screenshot
# Wait and select Visa Type
try:
visa_type_dropdown = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//label[text()='Visa
Type *']/following-sibling::span//select"))
)
visa_type_select = Select(visa_type_dropdown)
visa_type_select.select_by_index(2) # Change index if
necessary
print("Selected visa type")
except Exception as e:
print(f"Error selecting visa type: {e}")
# Click the Submit button
try:
submit_button = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH,
"//button[text()='Submit']"))
)
submit_button.click()
print("Clicked submit button.")
except Exception as e:
print(f"Error clicking submit button: {e}")
# Observe the result
time.sleep(5) # Wait for potential page load after form submission
final_url = driver.current_url
print(f"Final URL: {final_url}")
# Check if the link is opened, if not, wait for it
expected_url =
"https://www.blsspainmorocco.net/MAR/Appointment/VisaType"
check_link_open(expected_url, driver)
finally:
# Close the driver
driver.quit()
print("Driver closed.")