selenium_5.如何封装,封装思路

代码示例
用郑州大学

from selenium.webdriver.common.action_chains import ActionChains
'''面向过程编程写用例'''
# browser = webdriver.Chrome(executable_path=r'C:\Users\Administrator\Desktop\chromedriver.exe')
# browser.get("http://www.zzu.edu.cn/")           #打开郑州大学官网
# browser.switch_to.frame("frameid")              #切换frame
# yuanxizhuanye=browser.find_element_by_xpath("")  #院系专业
# ActionChains(browser).move_to_element(yuanxizhuanye).perform()  #鼠标移动
# browser.find_element_by_xpath("动物管理学的xpath").click()    #点击动物管理学的xpath

# hand=browser.window_handles      #获取窗口数
# browser.switch_to.window(hand[0]) #切换到第一个窗口
#time.sleep
# browser.switch_to.frame("frameid")   #切换frame
# yuanxizhuanye=browser.find_element_by_xpath("")  #院系专业
# ActionChains(browser).move_to_element(yuanxizhuanye).perform()  #鼠标移动
# browser.find_element_by_xpath("更多按钮的xpath").click()    #点击更多按钮的xpath

'''  第一次封装
面向函数写用例'''
# def move_to_yuanxizhuanye():
#     '''
#     移动到院系专业tab
#     :return:
#     '''
#     browser.switch_to.frame("frameid")  # 切换frame
#     yuanxizhuanye = browser.find_element_by_xpath("")  # 院系专业
#     ActionChains(browser).move_to_element(yuanxizhuanye)


# def switch_to_window(idx):
#     '''
#     切换到第几个窗口
#     :param idx: 窗口的索引
#     :return:
#     '''
#     hand = browser.window_handles
#     browser.switch_to.window(hand[idx])  # 切换到第一个窗口


# def click_element_until_present(value):
#     '''
#     等待元素出现,再点击元素
#     :return: 元素的xpath
#     '''
   #element=WebDriverWait(browser,10,0.3).until(EC.presence_of_element_located((By.XPATH,value)))
#     element.click()


#一层封装的用例
# browser = webdriver.Chrome(executable_path=r'C:\Users\Administrator\Desktop\chromedriver.exe')
# browser.get("http://www.zzu.edu.cn/")           #打开郑州大学官网
# click_element_until_present("点击 动物管理学的xpath")
# move_to_yuanxizhuanye()
# switch_to_window(0)
# move_to_yuanxizhuanye()
# click_element_until_present("更多按钮的xpath")


'''  第二次封装
面向对象写用例'''
class Case():
    def __init__(self):
        self.browser = webdriver.Chrome(executable_path=r'C:\Users\Administrator\Desktop\chromedriver.exe')
        self.browser.get("http://www.zzu.edu.cn/")  # 打开郑州大学官网

    def move_to_yuanxizhuanye(self):
        '''
        移动到院系专业tab
        :return:
        '''
        self.browser.switch_to.frame("frameid")  # 切换frame
        yuanxizhuanye = self.browser.find_element_by_xpath("")  # 院系专业
        ActionChains(self.browser).move_to_element(yuanxizhuanye)


    def switch_to_window(self,idx):
        '''
        切换到第几个窗口
        :param idx: 窗口的索引
        :return:
        '''
        hand = self.browser.window_handles
        self.browser.switch_to.window(hand[idx])  # 切换到第一个窗口


    def click_element_until_present(self,value):
        '''
        等待元素出现,再点击元素
        :return: 元素的xpath
        '''
        element=WebDriverWait(self.browser,10,0.3).until(EC.presence_of_element_located((By.XPATH,value)))
        element.click()

    def case(self):
        self.click_element_until_present("点击 动物管理学的xpath")
        self.move_to_yuanxizhuanye()
        self.switch_to_window(0)
        self.move_to_yuanxizhuanye()
        self.click_element_until_present("更多按钮的xpath")
# A=Case()
# A.case()


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值