Selenium+python automation - login case

foreword

If I don't write code for a day, I will panic in my free time. It's so uncomfortable...

The first few articles are about some basic knowledge. There are no specific cases. The friends seem to be boring. Many friends give suggestions to the editor and then give more specific details.

case. This article is to use the tribal forum as a test project to write a simple login test script.

When writing a login script, you must first ensure that the process can run, and then find a way to optimize the code to make your script look more comfortable and readable.

sex.

insert image description here

1. Login

  1.先打开浏览器

    2.打开github登录页:https://github.com/login

    3.查找元素之前可以先设置元素等待:implicitlywait()

    4.输入用户名、密码,然后点登录

insert image description here

code show as below:

Python学习交流Q群:906715085####
# 打开github首页
driver.get("https://github.com/login")
driver.implicitly_wait(10)
# 输入账号
driver.find_element_by_id("login_field").send_keys("youruser")
# 输入密码
driver.find_element_by_id("password").send_keys("yourpsw")
driver.find_element_by_name("commit").click()

insert image description here

2. Inspection results

1.登录完成之后,需要检查是否登录成功,这里就需要有个检查点,我这边选择右上角的账户名是不是我刚才登录这个账号

insert image description here

2.先定位到右上角设置,通过.text方法获取这个元素的文本属性

3.判断获取到的值,与期望结果是否一致

4.符合预期结果测试通过

5.不符合预期结果测试不通过

insert image description here

Reference Code:

Python学习交流Q群:906715085###
# 登录成功后,获取我的账户名称
time.sleep(5)
# 点右上角设置
driver.find_element_by_css_selector(".HeaderNavlink.name.mt-1").click()
# 获取账户名称
time.sleep(1)
t = driver.find_element_by_css_selector(".dropdown-header.header-nav-current-user.css-truncate>.css-truncate-target").text
print("获取到我的账户名称:%s" % t)

if t == "yoyoketang":
    print("登录成功!")
else:
    print("登录失败!")

insert image description here

3. Log out

1.测试完之后,别忘了最后退出登录

2.退出登录后,关闭浏览器
# 点sign out退出登录
driver.find_element_by_css_selector(".dropdown-item.dropdown-signout").click()
driver.quit()

 

insert image description here

4. Login function

1.虽然上面的代码能实现登录,但整个代码跟记流水账一样,没什么可读性。如果我想换个账号登录,这时候还得找到登录的账号和密码位置,比较费时。

2.我们可以把登录和退出写出两个函数,这样看起来更舒服一点。

3.把登录的账号和密码参数化
# coding:utf-8
from selenium import webdriver
import time

def login(driver, user, password):
    '''登录github'''
    
    driver.get("https://github.com/login")
    driver.implicitly_wait(10)
   
    driver.find_element_by_id("login_field").send_keys(user)
   
    driver.find_element_by_id("password").send_keys(password)
    driver.find_element_by_name("commit").click()

def logout(driver):
    '''退出github'''
    time.sleep(3)
    
    driver.find_element_by_css_selector(".HeaderNavlink.name.mt-1").click()
    time.sleep(1)
    # 点sign out
    driver.find_element_by_css_selector(".dropdown-item.dropdown-signout").click()
    driver.quit()

if __name__ == "__main__":
    driver = webdriver.Firefox()
    
    login(driver, "youruser", "yourpsw")
    print("hello  yoyo!")
    
    logout(driver

At last

How can good things not be shared with you? For those who are unclear about the case, remember to comment and leave a message, and solve the problem better.

Help us in the next study. I won't say more about the likes and favorites. The Selenium+python automated login case shared today is here

It's time to say goodbye to everyone.

insert image description here

Guess you like

Origin blog.csdn.net/xff123456_/article/details/124430925