- Python Automation Cookbook
- Jaime Buelta
- 162字
- 2021-08-13 15:51:16
How to do it...
- Import Selenium, start a browser, and load the form page. A page will open reflecting the operations:
>>> from selenium import webdriver
>>> browser = webdriver.Chrome()
>>> browser.get('https://httpbin.org/forms/post')
Note the banner with Chrome is being controlled by automated test software.
- Add a value in the Customer name field. Remember that it is called custname:
>>> custname = browser.find_element_by_name("custname")
>>> custname.clear()
>>> custname.send_keys("Sean O'Connell")
The form will update:
- Select the pizza size as medium:
>>> for size_element in browser.find_elements_by_name("size"):
... if size_element.get_attribute('value') == 'medium':
... size_element.click()
...
>>>
This will change the pizza size ratio box.
- Add bacon and cheese:
>>> for topping in browser.find_elements_by_name('topping'):
... if topping.get_attribute('value') in ['bacon', 'cheese']:
... topping.click()
...
>>>
Finally, the checkboxes will appear as marked:
- Submit the form. The page will submit and the result will be displayed:
>>> browser.find_element_by_tag_name('form').submit()
The form will be submitted and the result from the server will be displayed:
- Close the browser:
>>> browser.quit()