Automation Python + Selenium - mounting module and a driving method of operating the browser

Automation Python + Selenium - mounting module and a driving method of operating the browser


1. Install module file

pip install selenium

2. Install the browser driver

  • We mainly use the browser driver has a chrome browser , Firefox browser , there is no interface chrome browser .

2.1.ChromeDriver drive

  • First, download and install the Chrome browser, this is very simple, not in the description of the.
  • ChromeDriver drive installation, installation ChromeDriver only, in order to drive Chrome browser, complete the appropriate action.

2.1.1. Links

2.1.2. Check the Chrome version

  • Click the Chrome menu "Help" → "About Google Chrome", you can view the version number of Chrome.

2.1.3 Download ChromeDriver

  • Open ChromeDriver's official website, according to our Chrome browser version, download the corresponding ChromeDriver drive.

2.1.4. Configuration Environment Variables

  • Under Windows, the download is complete, the ChromeDriver executable file onto chromedriver.exe under the Python Scripts directory.

2.2.GeckoDriver drive

  • For Firefox browser, we need to install another drive GeckoDriver.

2.2.1. Links

2.2.2 Download GeckoDriver

  • Find release GeckoDriver on GitHub, and then select the corresponding driver download according to their own systems and digits.

2.2.3. Configuration Environment Variables

  • In Windows, drag the file directly to the geckodriver.exe under the Python Scripts directory.

3. Examples

  • Check that the preparatory work perfect, simple tests carried out. Chrome using selenium and drive to open the specified web page url.
# 1-导入模块文件
from selenium import webdriver
# 2-初始化浏览器为chrome浏览器
brower = webdriver.Chrome()
# 3-这里我们打开的是百度首页
brower.get('https://www.baidu.com/')
# 4-打印下网页标题
print(brower.title)
# 5-关闭浏览器
brower.quit()

Upon completion, the program will output in the console: Baidu, you know

4. A method of operating a browser

  • Chrome browser driver, Firefox browser drive, no chrome browser interface method of operation
# 1.初始化浏览器为chrome浏览器
browser = webdriver.Chrome()

# 2. 初始化浏览器为firefox浏览器
browser = webdriver.Firefox()

# 3. 初始化浏览器为无界面的chrome浏览器
option = webdriver.ChromeOptions()
option.add_argument("headless")
driver = webdriver.Chrome(chrome_options=option)


1. Install module file

pip install selenium

2. Install the browser driver

  • We mainly use the browser driver has a chrome browser , Firefox browser , there is no interface chrome browser .

2.1.ChromeDriver drive

  • First, download and install the Chrome browser, this is very simple, not in the description of the.
  • ChromeDriver drive installation, installation ChromeDriver only, in order to drive Chrome browser, complete the appropriate action.

2.1.1. Links

2.1.2. Check the Chrome version

  • Click the Chrome menu "Help" → "About Google Chrome", you can view the version number of Chrome.

2.1.3 Download ChromeDriver

  • Open ChromeDriver's official website, according to our Chrome browser version, download the corresponding ChromeDriver drive.

2.1.4. Configuration Environment Variables

  • Under Windows, the download is complete, the ChromeDriver executable file onto chromedriver.exe under the Python Scripts directory.

2.2.GeckoDriver drive

  • For Firefox browser, we need to install another drive GeckoDriver.

2.2.1. Links

2.2.2 Download GeckoDriver

  • Find release GeckoDriver on GitHub, and then select the corresponding driver download according to their own systems and digits.

2.2.3. Configuration Environment Variables

  • In Windows, drag the file directly to the geckodriver.exe under the Python Scripts directory.

3. Examples

  • Check that the preparatory work perfect, simple tests carried out. Chrome using selenium and drive to open the specified web page url.
# 1-导入模块文件
from selenium import webdriver
# 2-初始化浏览器为chrome浏览器
brower = webdriver.Chrome()
# 3-这里我们打开的是百度首页
brower.get('https://www.baidu.com/')
# 4-打印下网页标题
print(brower.title)
# 5-关闭浏览器
brower.quit()

Upon completion, the program will output in the console: Baidu, you know

4. A method of operating a browser

  • Chrome browser driver, Firefox browser drive, no chrome browser interface method of operation
# 1.初始化浏览器为chrome浏览器
browser = webdriver.Chrome()

# 2. 初始化浏览器为firefox浏览器
browser = webdriver.Firefox()

# 3. 初始化浏览器为无界面的chrome浏览器
option = webdriver.ChromeOptions()
option.add_argument("headless")
driver = webdriver.Chrome(chrome_options=option)

Guess you like

Origin www.cnblogs.com/xiao-xue-di/p/11531882.html