本文简单列举关于selenium WebDriver常用方法,仅作为学习参考记录。
另附“Storm啊”大佬提供的Web UI自动化测试练习地址:http://www.stormtest.cn/
列举的WebDriver常用方法:
- 鼠标悬停
- 下拉框
- frame
- 文件上传
- 键盘 *** 作
- 滑块 *** 作
- 窗口切换及窗口信息
- 等待
- 警告窗处理
- javascript调用
from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support.select import Select from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By import time class ActionTest: def __init__(self): self.driver = webdriver.Chrome() self.driver.maximize_window() def endDel(self, timeout): time.sleep(timeout) self.driver.quit() def hoverTest(self): # 鼠标悬停 self.driver.get("https://www.baidu.com") time.sleep(1) setting = self.driver.find_element_by_id("s-usersetting-top") ActionChains(self.driver).move_to_element(setting).perform() def selectTest(self): # 下拉框 self.driver.get("http://www.stormtest.cn/selectTest/") name = self.driver.find_element_by_id("s1") Select(name).select_by_index(2) # Select(name).select_by_value("o3") def frame(self): # frame self.driver.get("https://mail.qq.com/cgi-bin/loginpage?autologin=n&errtype=1") # self.driver.get("http://www.stormtest.cn/framesTest/") # 切换frame self.driver.switch_to.frame("login_frame") self.driver.find_element_by_id("u").send_keys(5555) # 切换回原基础页 self.driver.switch_to.default_content() def fileUploadTest(self): # 文件上传 self.driver.get("http://www.stormtest.cn/fileUploadTest/") time.sleep(3) self.driver.find_element_by_id("fileWdValidation").send_keys(r"D:test.txt") def wordKeys(self): # 键盘 *** 作 self.driver.get("http://www.baidu.com") kw = self.driver.find_element_by_id("kw") kw.send_keys("自动化测试项目高级实战Python") # backspace kw.send_keys(Keys.BACK_SPACE) # ctrl+axv kw.send_keys(Keys.CONTROL, 'a') kw.send_keys(Keys.CONTROL, 'x') kw.send_keys(Keys.CONTROL, 'v') kw.send_keys(Keys.CONTROL, 'v') def swipTest(self): # 滑块 *** 作 self.driver.get('https://passport.ctrip.com/user/reg/home') self.driver.find_element_by_css_selector("#agr_pop>div.pop_footer>a.reg_btn.reg_agree").click() time.sleep(5) sour = self.driver.find_element_by_css_selector("#slideCode > div.cpt-drop-box > div.cpt-drop-btn") ele = self.driver.find_element_by_css_selector("#slideCode > div.cpt-drop-box > div.cpt-bg-bar") # drag_and_drop_by_offse滑动(滑动源,X,Y) # ActionChains(self.driver).drag_and_drop_by_offset(sour, ele.size['width'], -sour.size['height']).perform() ActionChains(self.driver).drag_and_drop_by_offset(sour, 500, 0).perform() def switchWindowsTest(self): # 窗口切换及窗口信息 self.driver.get("http://www.stormtest.cn/") # 获取当前页面title、url title, url = self.driver.title, self.driver.current_url print({"title:": title, "url": url}) self.driver.find_element_by_link_text("窗口打开").click() # 获取Windows窗口及切换 self.driver.switch_to.window(self.driver.window_handles[-1]) bef_title, bef_url = self.driver.title, self.driver.current_url print({"title:": bef_title, "url": bef_url}) def waitTest(self): # # 设置隐式等待10s # self.driver.implicitly_wait(10) # 显式等待 self.driver.get("http://www.stormtest.cn/") # 5s最长超时时间,每0.5检测一次,until直到后面元素存在,超时抛异常 ele = WebDriverWait(self.driver, 5, 0.5).until(EC.visibility_of_element_located((By.link_TEXT, "链接(link)"))) ele.click() def alertTest(self): # 警告窗处理alert、/confirm/i、prompt # self.driver.get("http://www.stormtest.cn/alertTest/") # self.driver.get("http://www.stormtest.cn//confirm/iTest/") self.driver.get("http://www.stormtest.cn/promptTest/") self.driver.find_element_by_name("b1").click() # 获取警告窗并确认/取消 alert = self.driver.switch_to.alert alert.send_keys("这是promt框中输入的内容") alert.accept() # alert.dismiss() # 点击取消 def javascriptTest(self): # javascript调用 js = "window.scrollTo(200,500)" self.driver.get("https://www.baidu.com/s?wd=selenium") self.driver.execute_script(js) if __name__ == '__main__': action = ActionTest() action.javascriptTest() action.endDel(10)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)