文章目录
一、(鼠标)行为链
有时候在页面中的操作可能要有很多步,那么这时候可以使用鼠标行为链类ActionChains来完成。比如现在要将鼠标移动到某个元素上并执行点击事件
actions = ActionChains(driver)
actions.move_to_element(inputTag)
actions.send_keys_to_element(inputTag,'python')
actions.move_to_element(submitTag)
actions.context_click()
actions.click(submitTag)
actions.perform()
还有更多的鼠标相关的操作
• click_and_hold(element):点击但不松开鼠标。
• context_click(element):右键点击。
• double_click(element):双击。
• 更多方法请参考:http://selenium-python.readthedocs.io/api.html
具体使用:
from selenium import webdriver
from selenium.webdriver import ActionChains
import time
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
time.sleep(1)
# 定位输入框
inputTag = driver.find_element_by_id('kw')
# 定位百度一下的按钮
buttonTag = driver.find_element_by_id('su')
#鼠标行为链开始
# 实例化对象
actions = ActionChains(driver)
# 往输入框里面输入内容
actions.send_keys_to_element(inputTag,'浩哥')
time.sleep(1)
# 普通点击行为不要在鼠标行为链里面出现 buttonTag.click()
#点击行为
actions.move_to_element(buttonTag)
actions.click()
# 提交行为链
actions.perform()
#鼠标行为链结束
'''
1 千万不要忘记提交鼠标行为链
actions.perform()
2 需要注意鼠标行为链里面的点击操作
'''
二、selenium设置无界面模式(模板)
# 创建chrome设置对象
options = webdriver.ChromeOptions()
# 设置无界面功能 --headless 浏览器无界面 --xxxx
options.add_argument('--headless')
driver = webdriver.Chrome(options=options)
driver.get('https://maoyan.com/board/4')
三、selenium爬取数据
将使用到的一些方法或属性
drvier.page_source 获取html结构的

本文介绍Selenium自动化测试工具的应用技巧,包括鼠标行为链操作、无界面模式设置、数据爬取等,并提供猫眼电影TOP100及京东商品数据爬取的实战案例。
最低0.47元/天 解锁文章
1105

被折叠的 条评论
为什么被折叠?



