鼠标操作
鼠标事件对应的方法需要导入的类
from selenium.webdriver.common.action_chains import ActionChains
鼠标事件常用的操作方法
-
context_click() #鼠标右击
-
double_click() #鼠标双击
-
drag_and_drop() #按住拖拽
-
move_to_element() #鼠标悬停
-
perform() #执行以上事件方法
''' 右键 双击操作 ''' from selenium import webdriver from time import sleep from selenium.webdriver.common.action_chains import ActionChains # 获取浏览器对象 driver = webdriver.Chrome() driver.implicitly_wait(5) driver.maximize_window() # 获取网络链接 url = "https://www.baidu.com/" driver.get(url) # 实例化并获取ActionChains类 action = ActionChains(driver) # 定位搜索框,右击鼠标,将出现菜单 search = driver.find_element_by_css_selector("#kw") action.context_click(search).perform() sleep(2) # 搜索框中输入百度一下,然后鼠标双击,将会选中输入的文本 search.send_keys("百度一下") action.double_click(search).perform() sleep(2) driver.quit()
鼠标悬停操作
''' 鼠标悬停操作 ''' from selenium import webdriver from time import sleep from selenium.webdriver.common.action_chains import ActionChains # 获取浏览器对象 driver = webdriver.Chrome() driver.implicitly_wait(5) driver.maximize_window() # 获取网络链接 url = "https://www.baidu.com/" driver.get(url) # 实例化并获取ActionChains类 action = ActionChains(driver) # 定位设置。悬停在设置上 出现下拉框 set_el = driver.find_element_by_css_selector("#s-usersetting-top") action.move_to_element(set_el).perform() #定位搜索设置元素 点击 serch_set = driver.find_element_by_css_selector(".setpref").click() sleep(2) driver.quit()
鼠标拖拽(drag_and_drop)
from selenium import webdriver from time import sleep from selenium.webdriver.common.action_chains import ActionChains # 获取浏览器对象 driver = webdriver.Chrome() driver.implicitly_wait(5) driver.maximize_window() # 获取网络链接 url = "https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable" driver.get(url) #实例化ActionChains类 action = ActionChains(driver) #切换frame框 定位‘请拖拽我’元素 driver.switch_to.frame('iframeResult') start_el=driver.find_element_by_id('draggable') end_el=driver.find_element_by_id('droppable') #实现拖拽动作 action.drag_and_drop(start_el,end_el).perform() #弹窗处理 alert=driver.switch_to.alert alert.accept() sleep(3) driver.quit()
鼠标拖拽滑块操作 (drag_and_drop_by_offset)
from selenium import webdriver from time import sleep from selenium.webdriver.common.action_chains import ActionChains # 获取浏览器对象 driver = webdriver.Chrome() driver.implicitly_wait(5) driver.maximize_window() # 获取网络链接 url = "https://www.taobao.com/" driver.get(url) # 实例化并获取ActionChains类 action = ActionChains(driver) # 定位登录元素 点击 login_el = driver.find_element_by_css_selector(".h").click() #定位忘记密码元素 点击 rset_pwd_el = driver.find_element_by_css_selector(".forgot-password-a-link").click() #切换窗口 handles=driver.window_handles driver.switch_to_window(handles[1]) driver.find_element_by_id("J-accName").send_keys("18255676189") #1:定位滑块元素 start_el= driver.find_element_by_css_selector('#nc_1_n1z') #2:获取滑片的背景大小 background_size = driver.find_element_by_css_selector(".nc-lang-cnt").size start_location= start_el.location #3:.滑块的滑动范围[开始位置的横坐标加上滑片的宽度],由于是平移所以纵坐标没有变化 x_location = start_location["x"] + background_size["width"] y_location = start_location["y"] #利用鼠标拖拽操作 验证拖拽验证 # drag_and_drop 意思时 抓住 放下 需要填入 需要拖拽的元素 和 拖拽的终点 元素 action.drag_and_drop_by_offset(start_el,x_location,y_location).perform() sleep(3) driver.close() sleep(2) driver.quit()