Selenium Basics - Expansion: Use browser add-on configuration to realize user-free login

1. What is add-on configuration

In many cases, when we log in to a website, the browser will pop up a message whether to save the login account. If we choose to save, then we don't need to enter the account number again when we log in next time, and we can directly avoid logging in.

In our actual testing process, testing registration and login is only part of the process. However, when we open the website with selenium, we need to log in again every time, which is very troublesome. So can free login operation be realized in selenium?

Of course it is possible, you only need to configure the add-on for the browser to be opened.

Explain why selenium needs to log in again every time it opens a website.

When using the browser opened by selenium's webdriver, the script is used to open the browser. In fact, it reopens a process, which is not the same process as manually opening the browser, so some information will not be recorded.

2. Load Firefox configuration

illustrate:

Firefox browser loads browser configuration, need to use FirefoxProfile(profile_directory) to create a configuration class.

profile_directory is the path address of the browser configuration file.

How to find profile_directory?

Open the Firefox browser and click Settings >? (Help) > Troubleshooting Information > Show Folders

Go to the following interface:

 Example:

"""
1.学习目标:
    了解使用火狐浏览器实现免登陆
2.操作步骤
    1.手动登录网站,点击记住密码
    2.找火狐浏览器配置文件夹
        火狐浏览器--->设置--->帮助--->故障排除信息--->配置文件夹
        将文件夹路径复制保存
    3.将配置文件夹路径保存到代码
        profile_directory = 配置文件夹路径
    4.实例webdriver中的火狐浏览器profile
        profile = webdriver.FirefoxProfile(firefox_profile=配置文件夹路径)
    5.启动火狐浏览器,并传入配置信息
        driver = webdriver.Firefox(profile)
    6.打开可以免登陆的地址
        driver.get(网站)
3.需求
    使用火狐浏览器实现163邮箱免登陆
"""
# 1.导入selenium
from selenium import webdriver
from time import sleep
 
# 2.找火狐浏览器配置文件夹
# 火狐浏览器--->设置--->帮助--->故障排除信息--->配置文件夹
 
# 3.将配置文件夹路径保存到代码
# 如果不加r,路径中的\都要换成\\
profile_directory = r"C:\Users\L\AppData\Roaming\Mozilla\Firefox\Profiles\6pv0pces.default"
 
# 4.实例webdriver中的火狐浏览器profile
profile = webdriver.FirefoxProfile(profile_directory)
 
# 5.启动火狐浏览器,并传入配置信息
driver = webdriver.Firefox(firefox_profile=profile)
 
# 6.打开可以免登陆的地址
driver.get("http://mail.163.com/")
sleep(5)
 
# 7.关闭浏览器
driver.quit()

3. Load Chrome configuration

The principle is the same as that of the Firefox browser, and it can also be implemented in the Chrome browser.

However, some versions do not.

environment:

  • System environment: windows10
  • Python version: 3.7.7
  • Chrome browser version: 74.0.3729.131 (official version) (32-bit)

Steps:

   1. Get profile path

# Chrome安装路径
user-data-dir='C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\User Data\\'

Note: User DataMany people cannot find this directory, so it may not be possible.

  2. Load configuration data

# 配置谷歌浏览器加载项
options = webdriver.ChromeOptions()
options.add_argument(user_data_dir)

 3. Configure the add-on to start the browser

driver = webdriver.Chrome(options=options)

Example:

"""
1.学习目标
    了解使用谷歌浏览器实现账号免登陆
2.操作步骤(语法)
    2.1 手动登录网站,点击记住密码
    2.2 找到谷歌浏览器个人资料路径
        一定要注意格式:
        user_data_dir =
        '--user-data-dir=C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\User Data\\'
    2.3 配置谷歌浏览器加载项
        option = webdriver.ChromeOptions()
        option.add_argument(路径)
    2.4 打谷歌浏览器并且传入options
        driver=webdriver.Chrome(options=options)
    2.5 打开可以免登陆的地址
        driver.get(网站)
3.需求
    使用谷歌浏览器实现163邮箱免登陆
"""
# 1.导入selenium
from selenium import webdriver
from time import sleep
 
# 2.获取谷歌浏览器个人资料路径
user_data_dir = r"--user-data-dir=C:\Users\L\AppData\Local\Google\Chrome\User Data"
 
# 3.配置谷歌浏览器加载项
options = webdriver.ChromeOptions()
options.add_argument(user_data_dir)
 
# 4.打开谷歌浏览器并且传入options
driver = webdriver.Chrome(options=options)
 
# 5.打开可以免登陆的地址
driver.get("http://mail.163.com/")
sleep(5)
 
# 6.关闭浏览器
driver.quit()

Guess you like

Origin blog.csdn.net/lzz718719/article/details/131669185