from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
class Pyse(object):
def __init__(self,browser='ie'):
if browser.lower() == 'chrome':
driver = webdriver.Chrome()
elif browser.lower() == 'firefox':
driver = webdriver.Firefox()
elif browser.lower() == 'ie':
driver = webdriver.Ie()
self.driver = driver
def get_element(self,css):
'''获取元素'''
by = css.split('=>')[0]
value = css.split('=>')[1]
self.wait_element(css)
if by == 'css':
element = self.driver.find_element_by_css_selector(value)
elif by == 'id':
element = self.driver.find_element_by_id(value)
elif by == 'name':
element = self.driver.find_element_by_name(value)
elif by == 'class':
element = self.driver.find_elements_by_class_name(value)
return element
def wait_element(self,css,timeout=10,poll=0.5): # timeout是超时时间,poll是步长,每隔多少秒扫描一次元素,超过10秒没有找到就报错
'''等待元素出现'''
by = css.split('=>')[0]
value = css.split('=>')[1]
if by == 'css':
WebDriverWait(self.driver,timeout,poll).until(EC.presence_of_element_located((By.CSS_SELECTOR,value)))# 传入一个元组,(By.CSS_SELECTOR,value)
elif by == 'id':
WebDriverWait(self.driver,timeout,poll).until(EC.presence_of_element_located((By.ID,value)))
elif by == 'xpath':
WebDriverWait(self.driver,timeout,poll).until(EC.presence_of_element_located((By.XPATH,value)))
elif by == 'name':
WebDriverWait(self.driver,timeout,poll).until(EC.presence_of_element_located((By.NAME,value)))
elif by == 'class':
WebDriverWait(self.driver,timeout,poll).until(EC.presence_of_element_located((By.CLASS_NAME,value)))
def click(self,css):
'''点击操作'''
ele = self.get_element(css)
ele.click()
def type(self,css,value):
'''发送文本内容'''
ele = self.get_element(css)
ele.send_keys(value)
def get(self,url):
'''请求url方法'''
self.driver.get(url)
def max_window(self):
self.driver.maximize_window()
def clear(self,css):
ele = self.get_element(css)
ele.clear()
def move_to_element(self,css):
ele = self.get_element(css)
ActionChains(self.driver).move_to_element(ele).perform()
def drag_to_drop(self,source,target): # 拖拽操作,拼图
source_element = self.get_element(source)
target_element = self.get_element(target)
ActionChains(self.driver).drag_and_drop(source_element,target_element).perform()
def close(self):
self.driver.close()
def quit(self):
self.driver.quit()
def refresh(self): # 刷新
self.driver.refresh()
def back(self): # 后退
self.driver.back()
def forward(self): # 前进
self.driver.forward()
def select_to_value(self,css,value):
sele = self.get_element(css)
Select(sele).select_by_value(value) # Select实例化的时候必须接收一个select标签,select_by_value方法也接收一个value
@property
def window_handles(self):
return self.driver.window_handles
def window(self,i):
self.driver.switch_to.window(self.window_handles[i])
def iframe(self,css):
ifra = self.get_element(css)
self.driver.switch_to.frame(ifra)
def parent(self):
self.driver.switch_to.parent_frame()
def default_content(self):
self.driver.switch_to.default_content()
def accept(self): # 点击confirm按钮,关闭提示框,确认
self.driver.switch_to.alert.accept()
def dismiss(self): # 点击confirm按钮,关闭提示框,取消
self.driver.switch_to.alert.dismiss()
@property
def alert_text(self): # 获取文案,在pycharm里打印文案
return self.driver.switch_to.alert.text
def screenshot_as_file(self,file_path): # 截图
self.driver.get_screenshot_as_file(file_path)
def wait_to_exception_save_file(self,css):
try:
self.wait_element(css)
except Exception as e:
self.screenshot_as_file()
def js(self,js):
self.driver.execute_script(js)
if __name__ == '__main__':
pyse = Pyse()
# pyse.get('http://ui.imdsx.cn/uitester/')
pyse.get('http://ui.imdsx.cn/move/')
pyse.drag_to_drop('css=>#dragger','css=>#i1')
pyse.quit()
# pyse.type('css=>#i1',1111)
# element = pyse.get_element('css=>#i1')
# pyse.wait_element('css=>#i1',20,2)