Python+Selenium自动化-清空输入框、输入内容、点击按钮
1.输入内容
- send_keys('valve'):输入内容valve
input_box = browser.find_element_by_id('kw')
try:
input_box.send_keys('selenium')
print('搜索关键词:selenium')
except Exception as e:
print('fail')
2.点击按钮
button = browser.find_element_by_id('su')
try:
button.click()
print('成功搜索')
except Exception as e:
print('fail搜索')
3.清空输入框
try:
input_box.clear()
print('成功清空输入框')
except Exception as e:
print('fail清空输入框')
4.输入内容、点击按钮、清空输入完整实例
from selenium import webdriver
import time
browser = webdriver.Chrome()
browser.maximize_window()
browser.get('https://www.baidu.com')
input_box = browser.find_element_by_id('kw')
try:
input_box.send_keys('selenium')
print('搜索关键词:selenium')
except Exception as e:
print('fail')
button = browser.find_element_by_id('su')
try:
button.click()
print('成功搜索')
except Exception as e:
print('fail搜索')
try:
input_box.clear()
print('成功清空输入框')
except Exception as e:
print('fail清空输入框')
5.submit():模拟‘回车’操作
- submit():通常应用于提交表单;例如:搜索框输入内容后的回车操作。
from selenium import webdriver
import time
browser = webdriver.Chrome()
browser.maximize_window()
browser.get('https://www.baidu.com')
input_box = browser.find_element_by_id('kw')
input_box.send_keys('selenium')
try:
input_box.submit()
print('成功回车')
except Exception as e:
print('fail')
转载于:https://www.cnblogs.com/xiao-xue-di/p/11531905.html