前言:
如今你们对“selenium基本操作”都比较关心,你们都想要学习一些“selenium基本操作”的相关内容。那么小编同时在网摘上收集了一些关于“selenium基本操作””的相关内容,希望朋友们能喜欢,大家快快来了解一下吧!在功能的自动化测试中,经常会用到鼠标事件。在selenium中,我们主要借助的是action_chains模块中的ActionChains,使用该类要先进行导入,导入方式为:
from selenium.webdriver.common.action_chains import ActionChains
下面是详细源代码
1、鼠标悬浮操作
#/usr/bin/env python
#-*-coding:utf-8-*-
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time as t
from selenium.webdriver.common.keys import Keys
driver=webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get(";)
#实例化ActionChains
actionChains=ActionChains(driver)
element=driver.find_element_by_link_text("设置")
#鼠标悬浮操作 move_to_element()
actionChains.move_to_element(element).perform()
t.sleep(3)
driver.find_element_by_link_text("搜索设置").click()
t.sleep(3)
driver.quit()
2、鼠标右键操作
#/usr/bin/env python
#-*-coding:utf-8-*-
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time as t
from selenium.webdriver.common.keys import Keys
driver=webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get(";)
#实例化ActionChains
actionChains=ActionChains(driver)
element=driver.find_element_by_id("kw")
#鼠标右键操作context_click()
actionChains.context_click(element).perform()
t.sleep(3)
driver.quit()
3、鼠标右键
#/usr/bin/env python
#-*-coding:utf-8-*-
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time as t
from selenium.webdriver.common.keys import Keys
driver=webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get(";)
#实例化ActionChains
actionChains=ActionChains(driver)
driver.find_element_by_id("kw").send_keys('seleniun')
element=driver.find_element_by_id("su")
#鼠标双击操作double_click()
actionChains.double_click(element).perform()
t.sleep(3)
driver.quit()
4、键盘事件实战
#/usr/bin/env python
#-*-coding:utf-8-*-
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time as t
from selenium.webdriver.common.keys import Keys
driver=webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get(";)
# #实例化ActionChains
actionChains=ActionChains(driver)
re=driver.find_element_by_id('kw')
re.send_keys('Selenium')
#选中搜索框的关键字ctrl+a
re.send_keys(Keys.CONTROL,'a')
t.sleep(3)
#复制搜索框的关键字ctrl+c
re.send_keys(Keys.CONTROL,'c')
t.sleep(3)
#按下Backspace删除搜索关键字
re.send_keys(Keys.BACKSPACE)
t.sleep(3)
#打开Bing搜索首页
driver.get(';)
Bre=driver.find_element_by_id('sb_form_q')
#复制关键字到bing搜索框ctrl+v
Bre.send_keys(Keys.CONTROL,'v')
t.sleep(3)
driver.quit()
标签: #selenium基本操作