首页 > 代码库 > selenium之封装登陆操作

selenium之封装登陆操作

 

# selenium 封装登录操作举例
import os, time


# from selenium import webdriver

class LoginPage():
    ‘‘‘登录模块‘‘‘

    def __init__(self, path=‘‘):
        ‘‘‘初始化加载驱动‘‘‘
        if path:
            chrome_path = path
        else:
            chrome_path = "C:\\Users\\nriet\AppData\Local\Google\Chrome\Application\chrome.exe"
        os.environ["webdriver.chrome.bin"] = os.path.abspath(chrome_path)  # 浏览器安装路径
        self.driver_path = "D:\\Program Files\\Python36\\chromedriver.exe"  # 驱动路径
        # self.driver = webdriver.Chrome(executable_path=self.driver_path)
        self.driver = ‘‘

    def login(self, driver):
        ‘‘‘登录操作‘‘‘
        self.driver = driver
        self.driver.get("http://192.168.20.179:8080/YZWXTQ/login.do")
        time.sleep(1)
        self.driver.maximize_window()
        self.driver.find_element_by_id(‘name‘).send_keys(‘username‘)
        self.driver.find_element_by_id(‘password‘).send_keys(‘password‘)
        self.driver.find_element_by_id(‘login-btn‘).click()
        time.sleep(1)
        return self.driver

    def set_driver(self, driver):
        self.driver = driver

    def get_driver(self):
        return self.driver

    def quit(self):
        time.sleep(2)
        self.driver.quit()

 

selenium之封装登陆操作