Foreword
Some friends found that the originally downloaded plug-in was missing when they started the browser with a script, and they could not use firebug to continue to locate page elements on the opened page, which was inconvenient to debug.
加载浏览器配置,需要用FirefoxProfile(profile_directory)这个类来加载,
profile_directory is both the path address of the browser configuration file
1. Problems encountered
1.在使用脚本打开浏览器时候,发现右上角原来下载的插件firebug不见了,到底去哪了呢?
2.用脚本去打开浏览器时候,其实是重新打开了一个进程,跟手动打开浏览器不是一个进程。
Therefore, the plug-in is not actively loaded, but selenium actually provides a corresponding method to open it, but few people use it.
二、FirefoxProfile
1.要想了解selenium里面API的用法,最好先看下相关的帮助文档打开cmd窗口,
Enter the following information:
->python
-》from selenium import webdriver
-》help(webdriver.FirefoxProfile)
Help on class FirefoxProfile in module
selenium.webdriver.firefox.firefox_profile:
class FirefoxProfile(builtin.object)
| Methods defined here:
|
| init(self, profile_directory=None)
| Initialises a new instance of a Firefox Profile
|
| :args:
| - profile_directory: Directory of profile that you want to use.
| This defaults to None and will create a new
| directory when object is created.
2.翻译过来大概意思是说,这里需要profile_directory这个配置文件路径的参数
3.profile_directory=None,如果没有路径,默认为None,启动的是一个新的,
If there is, load the specified path.
三、profile_directory
1.问题来了:Firefox的配置文件地址如何找到呢?
2.打开Firefox点右上角设置>?(帮助)>故障排除信息>显示文件夹
3.打开后把路径复制下来就可以了:
C:\Users\xxx\AppData\Roaming\Mozilla\Firefox\Profiles\1x41j9of.default
Fourth, the startup configuration file
1.由于文件路径存在字符:\ ,反斜杠在代码里是转义字符,这个有点代码基础的应该都知道。
If you don't know what an escape character is, turn the book and make up the foundation yourself!
2.遇到转义字符,为了不让转义,有两种处理方式:
The first: \ (preceded by a backslash)
The second: r"\" (add r in front of the string, use the string prototype)
5. Reference code:
# coding=utf-8
from selenium import webdriver
# Profile address
profile_directory = r'C:\Users\xxx\AppData\Roaming\Mozilla\Firefox\Profiles\1x41j9of.default'
# Load configuration configuration
profile = webdriver.FirefoxProfile(profile_directory )
# start browser configuration
driver = webdriver.Firefox(profile)
本章很简单,多加2行代码而已,主要是弄清楚原理