UI自动化(四)-鼠标操作

鼠标操作
actionChains
操作+执行
点击 click
双击 double_click
悬浮 move_to_element
右键 context_click
拖曳 drag_and_drop
暂停 pause

移动鼠标到某个元素(悬浮)

1.找到鼠标要操作的元素对象
loc = (By.XPATH,"")
ele = driver.find_element(*loc)
2.实例化actionChains类
ac = ActionChains(driver)
3.调用鼠标操作
ac.move_to_element(ele).click(ele)
4.调用perform()去执行动作
ac.perform()

等待下拉列表可见,操作

loc = (By.XPATH,"")#例如百度首页-设置-高级搜索
wait = WebDriverWait(driver,20)
wait.until(EC.visibility_of_element_located(loc))
driver.find_element(*loc).click()
#点击元素,出现下拉列表(点击高级搜索后的框)
loc = (By.XPATH,"")
wait = WebDriverWait(driver,20)
wait.until(EC.visibility_of_element_located(loc))
driver.find_element(*loc).click()
#等待下拉列表中要操作的元素可见,操作
loc = (By.XPATH,"")
wait = WebDriverWait(driver,20)
wait.until(EC.visibility_of_element_located(loc))
driver.find_element(*loc).click()

选择下拉列表

1.实例化Select类-select元素。
loc1=(By.ID,’"hobby’)
WebDriverWait(driver,20).until(EC.visibility_of_element_located(loc1))
select_ele = driver.find_element(*loc1)
s = Select(select_ele)
2.使用它提供的选择方法,选择下拉列表的值
1)下标。 s.select_by_index()
2)文本。 s.select_by_visible_text()
3)value属性 s.select_by_value()

你可能感兴趣的:(自动化)