毫无疑问,首先需要导入webdriver
from selenium import webdriver
清除文本
driver.find_element_by_id('kw').clear()文本输入
driver.find_element_by_id('kw').send_keys("selenium")按键输入
from selenium.webdriver.common.keys import Keys driver.find_element_by_id('kw').send_keys(Keys.ENTER) # 回车组合按键输入
from selenium.webdriver.common.keys import Keys driver.find_element_by_id('kw').send_keys(Keys.CONTROL, 'a') # 全选点击元素
driver.find_element_by_id('su').click()提交表单
driver.find_element_by_id('su').submit()获取元素的文本
driver.find_element_by_name('tj_trnews').text获取元素的尺寸
driver.find_element_by_name('tj_trnews').size获取元素属性值
driver.find_element_by_id('kw').get_attribute("autocomplete")判断元素是否用户可见
driver.find_element_by_id('kw').is_displayed()鼠标右键
from selenium.webdriver.common.action_chains import ActionChains # 定位到要右击的元素 mask = driver.find_element_by_id('xxx') # 鼠标右键 ActionChains(driver).context_click(mask).perform()鼠标双击
from selenium.webdriver.common.action_chains import ActionChains # 定位到要右击的元素 mask = driver.find_element_by_id('xxx') # 鼠标右键 ActionChains(driver).double_click(mask).perform()鼠标拖放
from selenium.webdriver.common.action_chains import ActionChains # 定位元素的原位置 source = driver.find_element_by_name('source') # 定位元素要移动到的目标位置 target = driver.find_element_by_name('target') # 执行元素移动操作 ActionChains(driver).drag_and_drop(source, target).perform()上传文件
driver.find_element_by_name("file").send_keys('D:\\upload_file.txt')下拉框选择
<select id="city"> <option value="bj">北京</option> <option value="gz">广州</option> <option value="sh">上海</option> </select> # 先定位到下拉框 select = driver.find_element_by_id('city') # 再点击下拉框下的选项 select.find_element_by_xpath('//option[@value="gz"]').click()对话框alert、confirm、prompt 的处理
#定位到弹窗窗口并接受警告信息 driver.switch_to.alert.accept() #定位到弹窗窗口获取警告文本信息 driver.switch_to.alert.text #定位到弹窗窗口并取消警告 driver.switch_to.alert.dismiss() # 定位到弹窗窗口并输入确认信息 driver.switch_to.alert.send_keys("xxx")
转载于:https://www.cnblogs.com/guanfuchang/p/7434866.html
相关资源:selenium-python中文文档.pdf