selenium编程02-简单的PO模式登陆163邮箱

from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
class Page():
    login_url='https://mail.163.com'
    def __init__(self,driver,base_url=login_url):
        self.driver=driver
        self.base_url=base_url
        self.timeout=30
    def on_page(self):
        return self.driver.current_url==(self.base_url+self.url)
    def _open(self,url):
        url=self.base_url+url
        self.driver.get(url)
        sleep(6)
        assert self.on_page(),'Did not land on %s' %url
    def open(self):
        self._open(self.url)
    def find_element(self,*loc):
        return self.driver.find_element(*loc)
class LoginPage(Page):
    url='/'
    start_loc=(By.ID,'switchAccountLogin')
    if_loc=(By.XPATH,'//iframe[starts-with(@id,"x-URS-iframe")]')
    username_loc = (By.NAME, 'email')
    password_loc = (By.NAME, 'password')
    submit_loc=(By.ID,'dologin')
    def start_click(self):
        return self.find_element(*self.start_loc).click()
    def type_username(self,username):
        return self.find_element(*self.username_loc).send_keys(username)
    def type_password(self,password):
        return self.find_element(*self.password_loc).send_keys(password)
    def submit(self):
        return self.find_element(*self.submit_loc).click()
    def switch_toiframe(self):
        If=self.find_element(*self.if_loc)
        self.driver.switch_to.frame(If)
def test_user_login(driver,username,password):
    login_page=LoginPage(driver)
    login_page.open()
    login_page.start_click()
    sleep(3)
    login_page.switch_toiframe()
    sleep(3)
    login_page.type_username(username)
    sleep(3)
    login_page.type_password(password)
    login_page.submit()
def main():
    try:
        driver=webdriver.Firefox(executable_path='D:/Program Files/Mozilla Firefox/geckodriver')
        username='xueshanfeihu1992'
        password='hf13516458506'
        test_user_login(driver,username,password)
        sleep(4)
    finally:
        driver.quit()
if __name__=='__main__':
    main()

 

转载于:https://www.cnblogs.com/feihujiushiwo/p/10969080.html

你可能感兴趣的:(selenium编程02-简单的PO模式登陆163邮箱)