直接学习:https://edu.csdn.net/course/play/24756/284647
selenium行为链:
有时候在页面的操作中可能要有很多步,那么这时候可以使用鼠标行为链类selenium.webdriver.common.action_chains . ActionChains来完成。比如现在要将鼠标移动到某个元素上并执行点击事件。
示例代码如下:`
driver.get('https://www.baidu.com')
inputTag = driver.find_element_by_id('kw')
submitTag = driver.find_element_by_id('su')
actions = ActionChains(driver)
actions.move_to_element(inputTag)
actions.send_keys_to_element(inputTag,"python")
actions.move_to_element(submitTag)
actions.click()
# time.sleep()
actions.perform()
还有更多的鼠标的相关操作:
click_and_hold(element):点击但不松开鼠标
context_click(element):右键点击
double_click(element):双击
#为什么需要行为链条?#
因为有些网站可能会在浏览器做一些验证行为是否符合人类的行为的反爬虫手段,这时候我们就可以使用行为链来模拟人的操作。行为链有更多复杂操作,比如双击,右键等,在自动化测试中非常有用。