Background: Recurring, in the project we need to open different browsers for different configurations. In the process of learning selenium, open the local Firefox and local Chrome is a bit of a hassle, the Java version of the Internet is a lot of information, but the Python version of the few, here, I studied a python version of selenium open the browser of the document for their own notes , but also hope to give you some reference.
1. Open the default Firefox
Browser = Webdriver. Firefox ()
2. Open the locally configured Firefox
From selenium import Webdriver
From time import sleep
Import OS
Profiledir = "C:users\\cui\\appdata\\roaming\\mozilla\\firefox\\profiles\\vrpxe102.default"
Profile = Webdriver. Firefoxprofile (Profiledir)
Browser = Webdriver. Firefox (Profile)
Analytical:
The first part is to give the local configuration address, this address in the Firefox browser installation directory, the relative path is firefox\\profiles\\vrpxe102.default. That is the. default file. The name of the. Default file is different under each browser.
The second part reads the local configuration. The third part, which is to use this local configuration, to open the local Firefox.
3. Open the default Chrome
In the Python version of Webdriver to open the Chrome browser, you need to install Chromedriver.exe.
Place this chromedriver.exe in the Chrome installation directory relative to the path to ... \google\chrome\application\, then set the PATH environment variable to put Chrome's installation directory address into the PATH variable. My installation directory is C:\Program Files\google\chrome\application, that is, at the end of the path variable, add this directory, and then try to run it in Python
From selenium import Webdriver
Driver = Webdriver. Chrome ()
If the error is not, the installation is successful.
4. Open the local chrome
Import OS
From selenium import Webdriver
From selenium.webdriver.chrome.options Import options
From time import sleep
Executable_path = "C:\Program Files (x86) \google\chrome\application\chromedriver.exe"//Specify the location of Chromediver, if in the default path, These two lines can be omitted.
os.environ["Webdriver.chrome.driver"] = Executable_path
Options = Webdriver. Chromeoptions ()//Define Configuration objects
Options.add_argument ("--user-data-dir=" +r "C:/users/cui/appdata/local/google/chrome/user Data/")
Specify the user's configuration address and load it into the configuration object.
Browser = Webdriver. Chrome (executable_path,chrome_options=options)
Note here that there are two parameters, after that parameter, must be written in the form of chrome_options=xx, otherwise run an error.
Note: Chrome's user Configuration data, in the C drive under the user, some people may not be able to find this directory, because the Appdate folder is hidden by default, you need to go to the folder options to set up a bit.
Open Local browser version--python using Webdriver