1. 程式人生 > >selenium之封裝登陸操作

selenium之封裝登陸操作

.get rom chrome exe log environ cut path div

# 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之封裝登陸操作