-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform.py
More file actions
74 lines (63 loc) · 2.32 KB
/
form.py
File metadata and controls
74 lines (63 loc) · 2.32 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
# Selenium Curse
# Import necessary modules
import os
from selenium import webdriver
from selenium.webdriver.edge.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
from selenium.common.exceptions import TimeoutException
# Configure WebDriver options
edge_options = Options()
# Set the path to the adblocker extension
extension_path = os.path.join(os.getcwd(), ".gitignore", "adBlocker.crx")
# Add adblocker extension
edge_options.add_extension(extension_path)
# Initialize the WebDriver
driver = webdriver.Edge(options=edge_options)
# Target web page to automate
URL = "https://practice.expandtesting.com/form-validation"
# Open the target web page
driver.get(URL)
# Close the adblocker tab
for handle in driver.window_handles:
driver.switch_to.window(handle)
if "adblockultimate.net" in driver.current_url:
driver.close()
break
# Switch back to the main window
driver.switch_to.window(driver.window_handles[0])
# Fill out the form fields
try:
# Locate the contact name field
contact_name = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.NAME, "ContactName"))
)
contact_name.clear()
contact_name.send_keys("John Doe")
# Locate the contact number field
contact_number = driver.find_element(By.NAME, "contactnumber")
contact_number.clear()
contact_number.send_keys("098-1234567")
# Locate Pickup Date field
pickup_date = driver.find_element(By.NAME, "pickupdate")
pickup_date.send_keys("12/12/2024")
# Locate the Payment Method dropdown
payment_method = driver.find_element(By.NAME, "payment")
for option in payment_method.find_elements(By.TAG_NAME, "option"):
if option.text == "card":
option.click()
break
# Locate and click the Register button
register_button = driver.find_element(By.CSS_SELECTOR, ".btn.btn-primary")
register_button.click()
# Wait form was submitted and confirmation page loaded
confirmation_page = WebDriverWait(driver, 10).until(
EC.url_contains("form-confirmation")
)
print("Form submitted successfully")
except TimeoutException:
print("Timeout.Element not found.")
# Cleanup
finally:
driver.quit()