Article From:https://www.cnblogs.com/laosun0204/p/9121967.html
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):
'''Get the element '' ''
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): # timeoutIt's timeout time. Poll is the step length, scanning elements once every few seconds, and failing to find it in more than 10 seconds.
'''Waiting for the appearance of the element ''
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)))# Pass into a tuple, (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):
'''Click operation ''
ele = self.get_element(css)
ele.click()

def type(self,css,value):
'''Send text content ''
ele = self.get_element(css)
ele.send_keys(value)

def get(self,url):
'''Request the URL method ''
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): # Drag and drop operations, jigsaw puzzle
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): # Refresh
self.driver.refresh()

def back(self): # Back off
self.driver.back()

def forward(self): # Forward
self.driver.forward()

def select_to_value(self,css,value):
sele = self.get_element(css)
Select(sele).select_by_value(value) # SelectWhen instantiating, you must receive a select tag, and the select_by_value method also receives a 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): # Click the confirm button, turn off the prompt box, and confirm
self.driver.switch_to.alert.accept()

def dismiss(self): # Click the confirm button, turn off the prompt box, cancel
self.driver.switch_to.alert.dismiss()

@property
def alert_text(self): # Get the copywriting, print the copywriting in pycharm
return self.driver.switch_to.alert.text

def screenshot_as_file(self):
self.driver.get_screenshot_as_file()

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)

 

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *