WebDriver元素定位操作
已百度输入框为例
一:常用操作指令
- 清除文本:clear()
- 模拟按键输入:send_keys(value) 点击:click()
- driver.find_element_by_id("kw").clear()
- driver.find_element_by_id("kw").send_keys("88")
- driver.find_element_by_id("kw").click()
- driver.find_element_by_id("kw").send_keys("88")
- search.submit()
二:鼠标操作指令
from selenium.webdriver import ActionChains
- 鼠标悬停操作:
- w = deriver.find_element_by_css_selector(”#s-top-right“)
- ActionChains(driver).move_to_elements(w).perform()
三:键盘组合键
from selenium.webdriver.common.keys inport Keys
- driver.find_selement_by_id("su”).send_keys(Keys.ENTER) #回车单击
- driver.find_selement_by_id("kw").send_keys(Keys.CONRTOL, 'x')#黏贴内容到输入框
- driver.find_selement_by_id("kw").send_keys(Keys.CONRTOL, 'v')#剪切输入框内容
- driver.find_selement_by_id("kw").send_keys(Keys.CONRTOL, 'a')
- driver.find_selement_by_id("kw").send_keys(Keys.SPACE')
- driver.find_selement_by_id("kw").send_keys("你好“) #输入空格键+你好
- driver.find_selement_by_id("kw").send_keys("你好”)
- driver.find_selement_by_id("su").send_keys(BACK.SPACE)#删除好字
- 其它
- send_keys(Keys.BACK_SPACE)#删除键
- send_keys(Keys.SPACE)#空格
- send_keys(Keys.TAB)#制表键
- send_keys(Keys.ESCAPE)#回退键
- send_keys(Keys.ENTER)#回车键
四:web页面信息获取
- a = driver.title#页面标题
- a = driver.current_url#当前页面URL
- a = drver.find_element_by_css('#kw').text 获取当前页面的文本消息