Objective
A small partner in the script to launch the browser when the original downloaded plugin is missing, unable to use Firebug on the open page to continue to locate page elements, debugging inconvenient.
加载浏览器配置,需要用FirefoxProfile(profile_directory)这个类来加载,
Profile_directory both the path address of the browser configuration file
First, encounter problems
1.在使用脚本打开浏览器时候,发现右上角原来下载的插件firebug不见了,到底去哪了呢?2.用脚本去打开浏览器时候,其实是重新打开了一个进程,跟手动打开浏览器不是一个进程。
So did not take the initiative to load the plug-in, but selenium inside actually provides the corresponding method to open, but very few people use.
Second, Firefoxprofile
1.要想了解selenium里面API的用法,最好先看下相关的帮助文档打开cmd窗口,
Enter the following information:
-"Python
-"From selenium import Webdriver
-"Help" (Webdriver. Firefoxprofile)
Help on the 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 so want to use.
| This defaults to None and would create a new
| Directory when object is created.
2.翻译过来大概意思是说,这里需要profile_directory这个配置文件路径的参数
3.profile_directory=None,如果没有路径,默认为None,启动的是一个新的,
The specified path is loaded with some words.
Third, Profile_directory
1.问题来了:Firefox的配置文件地址如何找到呢?2.打开Firefox点右上角设置>?(帮助)>故障排除信息>显示文件夹
3.打开后把路径复制下来就可以了:
C:\Users\xxx\AppData\Roaming\Mozilla\Firefox\Profiles\1x41j9of.default
Iv. Boot configuration file
1.由于文件路径存在字符:\ ,反斜杠在代码里是转义字符,这个有点代码基础的应该都知道。
Do not understand what is called the escape character, the book to fill the foundation!
2.遇到转义字符,为了不让转义,有两种处理方式:
First type: \ (preceded by a backslash)
Second type: R "\" (string preceded by R, using string prototypes)
Five, reference code:
# Coding=utf-8
From selenium import Webdriver
# config file address
Profile_directory = R ' C:\Users\xxx\AppData\Roaming\Mozilla\Firefox\Profiles\1x41j9of.default '
# Load Configuration Configuration
Profile = Webdriver. Firefoxprofile (Profile_directory)
# Launch Browser configuration
Driver = Webdriver. Firefox (Profile)
本章很简单,多加2行代码而已,主要是弄清楚原理
Selenium2+python Automation 18-load Firefox configuration