Selenium
Selenium
class EmbassyFormAutomation:
def __init__(self, gecko_path=None):
"""Initialize Firefox automation"""
# Set up Firefox options
firefox_options = Options()
# firefox_options.add_argument('--headless') # Uncomment to run in
headless mode
# Configure logging
logging.basicConfig(level=logging.INFO)
self.logger = logging.getLogger(__name__)
def navigate_to_form(self):
"""Navigate to the embassy appointment form"""
url = "https://service2.diplo.de/rktermin/extern/appointment_showForm.do?
locationCode=isla&realmId=108&categoryId=1600"
self.driver.get(url)
time.sleep(3) # Wait for page load
def accept_cookies(self):
"""Handle cookie consent if present"""
try:
cookie_button = self.wait.until(EC.presence_of_element_located(
(By.CSS_SELECTOR, "button[data-cookie-consent-accept]")))
cookie_button.click()
self.logger.info("Accepted cookies")
except TimeoutException:
self.logger.info("No cookie consent found")
pass
# Handle captcha
self.handle_captcha()
# Submit form
self.submit_form()
return True
except Exception as e:
self.logger.error(f"Error filling form: {str(e)}")
return False
def handle_captcha(self):
"""Handle the captcha on the form"""
try:
# Find captcha image
captcha_img = self.wait.until(EC.presence_of_element_located(
(By.CSS_SELECTOR, "img[src*='captcha']")))
# Extract text
captcha_text = pytesseract.image_to_string(image)
captcha_text = ''.join(e for e in captcha_text if e.isalnum())
self.logger.info("Captcha handled")
except Exception as e:
self.logger.error(f"Error handling captcha: {str(e)}")
def submit_form(self):
"""Submit the form"""
try:
submit_button = self.wait.until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, "input[type='submit']")))
submit_button.click()
time.sleep(2) # Wait for submission
self.logger.info("Form submitted")
except Exception as e:
self.logger.error(f"Error submitting form: {str(e)}")
def close(self):
"""Close the browser"""
self.driver.quit()
def main():
# Example form data
form_data = {
"familyName": "Doe",
"firstName": "John",
"email": "[email protected]",
"dateOfBirth": "01.01.1990", # Format: DD.MM.YYYY
"nationality": "INDIA", # Exact text as it appears in dropdown
"numberOfPersons": "1"
}
try:
bot = EmbassyFormAutomation()
bot.navigate_to_form()
bot.accept_cookies()
success = bot.fill_application_form(form_data)
if success:
print("Form submitted successfully")
else:
print("Form submission failed")
except Exception as e:
print(f"Error: {str(e)}")
finally:
bot.close()
if __name__ == "__main__":
main()