Use Selenium to control Chrome browser -- job automation
background:
Recently, when a friend was using Miaoaccount for accounting, due to the large volume of sales orders, the repetitive entry work lasted for nearly five hours a day. He asked for help and asked if the repetitive labor could be reduced. After reading it, he analyzed it and used web automation. Can solve the problem, hereby record as follows:
Introduction to Selenium: Selenium is a complete web application testing system, including test recording (Selenium IDE), writing and running (Selenium Remote Control) and parallel processing of tests (Selenium Grid). The core of Selenium, Selenium Core, is based on JsUnit and is written entirely in JavaScript, so it can be used on any browser that supports JavaScript. Languages supported by Selenium include C#, Java, Perl, PHP, Python, and Ruby. Currently, Selenium Web Driver is the most popular for Python. Selenium test scripts can be coded in any supported programming language and run directly in most modern web browsers.
Install Selenium:
pip install selenium
Check Chrome version, download and install chromedriver
The Windows version of chromedriver is divided into 32-bit and 64-bit. Students who use a 32-bit Windows operating system should download the Windows x86 version, and students who use a 64-bit Windows operating system should download the Windows x86-64 version.
Download address: http://chromedriver.storage.googleapis.com/index.html
Choose according to your browser:
After downloading, it is a compressed package. After decompression, there is only one file: chromedriver.exe, and put it in the scripts directory of the python installation directory. I put it directly in the specified directory when debugging, and added the code:
chrome_driver = os.path.join(os.getcwd(), "chromedriver.exe")
Equipment before commissioning:
# -*- coding: utf-8 -*-
# @Time : 2023年04月30日 20时51分
# @File : 秒帐.py
# @notice :
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
# chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222") # 9515--79
chrome_driver = os.path.join(os.getcwd(), "chromedriver.exe")
# print('kaishi', chrome_driver)
browser = webdriver.Chrome(executable_path=chrome_driver, options=chrome_options)
# browser.maximize_window() # 最大化窗口
# 通过浏览器向服务器发送URL请求
browser.get("http://baidu.com")
It works fine and can be debugged