本文简单列举关于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)
def frame(self):
self.driver.get("https://mail.qq.com/cgi-bin/loginpage?autologin=n&errtype=1")
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")
kw.send_keys(Keys.BACK_SPACE)
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")
ActionChains(self.driver).drag_and_drop_by_offset(sour, 500, 0).perform()
def switchWindowsTest(self):
self.driver.get("http://www.stormtest.cn/")
title, url = self.driver.title, self.driver.current_url
print({"title:": title, "url": url})
self.driver.find_element_by_link_text("窗口打开").click()
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):
self.driver.get("http://www.stormtest.cn/")
ele = WebDriverWait(self.driver, 5, 0.5).until(EC.visibility_of_element_located((By.LINK_TEXT, "链接(Link)")))
ele.click()
def alertTest(self):
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()
def javascriptTest(self):
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)
|