Form Automation
user_9132356
python
a year ago
1.2 kB
1
Indexable
Never
from selenium import webdriver from selenium.webdriver.common.keys import Keys # Set up the Selenium WebDriver environment driver = webdriver.Chrome() # Navigate to the URL of the web form you need to automate driver.get("https://example.com/form") # Find and fill in the form fields with predefined data name_field = driver.find_element_by_name("name") #Also can find using XPATH name_field.send_keys("John Doe") email_field = driver.find_element_by_name("email") email_field.send_keys("johndoe@example.com") message_field = driver.find_element_by_name("message") message_field.send_keys("This is a test message") # Trigger the submit action by clicking the submit button submit_button = driver.find_element_by_css_selector("button[type='submit']") submit_button.click() # Wait for the confirmation page to load confirmation_page_loaded = driver.wait.until( lambda driver: driver.current_url != "https://example.com/form" ) # Alternatively, verify the success message displayed after form submission success_message_displayed = driver.find_element_by_css_selector(".success-message").is_displayed() # Close the web browser driver.quit()