Trong bài viết này, chúng ta sẽ xem cách cài đặt Selenium webdriver trong python3 bằng các bước đơn giản. Webdriver là một API và giao thức hoạt động như một giao diện để kiểm soát hành vi của trình duyệt web. Mỗi trình duyệt được hỗ trợ bởi một triển khai WebDriver cụ thể, được gọi là trình điều khiển. Trình điều khiển là thành phần chịu trách nhiệm ủy quyền cho trình duyệt và xử lý giao tiếp đến và từ Selenium và trình duyệt. Selenium WebDriver đề cập đến cả ràng buộc ngôn ngữ và triển khai mã kiểm soát trình duyệt riêng lẻ. Điều này thường được gọi là WebDriver. Thêm trên trang web chính thức
Những đặc điểm quan trọng
- WebDriver được thiết kế dưới dạng giao diện lập trình đơn giản và ngắn gọn hơn
- WebDriver là một API hướng đối tượng nhỏ gọn
- Nó điều khiển trình duyệt một cách hiệu quả
Cách cài đặt Selenium WebDriver trong Python 3 [Các bước đơn giản]
Cũng đọc. Cách vẽ nhiều biểu đồ trong Python bằng Matplotlib
Bước 1. điều kiện tiên quyết
a] Bạn nên có Hệ thống
cyberithub@ubuntu:~$ sudo apt install python3-pip Reading package lists.. Done Building dependency tree Reading state information.. Done The following packages were automatically installed and are no longer required: libjs-jquery-ui pypy-lib python-matplotlib-data python3-cycler python3-kiwisolver Use 'sudo apt autoremove' to remove them. The following NEW packages will be installed: python3-pip 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 231 kB of archives. After this operation, 1,050 kB of additional disk space will be used. Get:1 //in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 python3-pip all 20.0.2-5ubuntu1.6 [231 kB] Fetched 231 kB in 11s [21.6 kB/s] Selecting previously unselected package python3-pip. [Reading database .. 258471 files and directories currently installed.] Preparing to unpack .../python3-pip_20.0.2-5ubuntu1.6_all.deb ... Unpacking python3-pip [20.0.2-5ubuntu1.6] ... Setting up python3-pip [20.0.2-5ubuntu1.6] ... Processing triggers for man-db [2.9.1-1] ...5 hoặc
cyberithub@ubuntu:~$ sudo apt install python3-pip Reading package lists.. Done Building dependency tree Reading state information.. Done The following packages were automatically installed and are no longer required: libjs-jquery-ui pypy-lib python-matplotlib-data python3-cycler python3-kiwisolver Use 'sudo apt autoremove' to remove them. The following NEW packages will be installed: python3-pip 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 231 kB of archives. After this operation, 1,050 kB of additional disk space will be used. Get:1 //in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 python3-pip all 20.0.2-5ubuntu1.6 [231 kB] Fetched 231 kB in 11s [21.6 kB/s] Selecting previously unselected package python3-pip. [Reading database .. 258471 files and directories currently installed.] Preparing to unpack .../python3-pip_20.0.2-5ubuntu1.6_all.deb ... Unpacking python3-pip [20.0.2-5ubuntu1.6] ... Setting up python3-pip [20.0.2-5ubuntu1.6] ... Processing triggers for man-db [2.9.1-1] ...6 đang chạy
b] Bạn phải có kết nối Internet đang hoạt động
c] Bạn phải có quyền truy cập để cài đặt gói trong Hệ thống của mình
Bước 2. Cài đặt Python3
Nếu bạn chưa cài đặt python3 trong Hệ thống của mình thì bạn có thể cài đặt nó trên hệ thống dựa trên Ubuntu/Debian bằng cách sử dụng lệnh
cyberithub@ubuntu:~$ sudo apt install python3-pip Reading package lists.. Done Building dependency tree Reading state information.. Done The following packages were automatically installed and are no longer required: libjs-jquery-ui pypy-lib python-matplotlib-data python3-cycler python3-kiwisolver Use 'sudo apt autoremove' to remove them. The following NEW packages will be installed: python3-pip 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 231 kB of archives. After this operation, 1,050 kB of additional disk space will be used. Get:1 //in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 python3-pip all 20.0.2-5ubuntu1.6 [231 kB] Fetched 231 kB in 11s [21.6 kB/s] Selecting previously unselected package python3-pip. [Reading database .. 258471 files and directories currently installed.] Preparing to unpack .../python3-pip_20.0.2-5ubuntu1.6_all.deb ... Unpacking python3-pip [20.0.2-5ubuntu1.6] ... Setting up python3-pip [20.0.2-5ubuntu1.6] ... Processing triggers for man-db [2.9.1-1] ...7 như hình bên dưới. Nếu bạn đang sử dụng các hệ thống dựa trên RHEL/CentOS thì bạn có thể sử dụng lệnh
cyberithub@ubuntu:~$ sudo apt install python3-pip Reading package lists.. Done Building dependency tree Reading state information.. Done The following packages were automatically installed and are no longer required: libjs-jquery-ui pypy-lib python-matplotlib-data python3-cycler python3-kiwisolver Use 'sudo apt autoremove' to remove them. The following NEW packages will be installed: python3-pip 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 231 kB of archives. After this operation, 1,050 kB of additional disk space will be used. Get:1 //in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 python3-pip all 20.0.2-5ubuntu1.6 [231 kB] Fetched 231 kB in 11s [21.6 kB/s] Selecting previously unselected package python3-pip. [Reading database .. 258471 files and directories currently installed.] Preparing to unpack .../python3-pip_20.0.2-5ubuntu1.6_all.deb ... Unpacking python3-pip [20.0.2-5ubuntu1.6] ... Setting up python3-pip [20.0.2-5ubuntu1.6] ... Processing triggers for man-db [2.9.1-1] ...0 hoặc
cyberithub@ubuntu:~$ sudo apt install python3-pip Reading package lists.. Done Building dependency tree Reading state information.. Done The following packages were automatically installed and are no longer required: libjs-jquery-ui pypy-lib python-matplotlib-data python3-cycler python3-kiwisolver Use 'sudo apt autoremove' to remove them. The following NEW packages will be installed: python3-pip 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 231 kB of archives. After this operation, 1,050 kB of additional disk space will be used. Get:1 //in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 python3-pip all 20.0.2-5ubuntu1.6 [231 kB] Fetched 231 kB in 11s [21.6 kB/s] Selecting previously unselected package python3-pip. [Reading database .. 258471 files and directories currently installed.] Preparing to unpack .../python3-pip_20.0.2-5ubuntu1.6_all.deb ... Unpacking python3-pip [20.0.2-5ubuntu1.6] ... Setting up python3-pip [20.0.2-5ubuntu1.6] ... Processing triggers for man-db [2.9.1-1] ...1. Nếu bạn đang sử dụng các hệ thống dựa trên Windows, hãy truy cập trang web chính thức và tải xuống và cài đặt phần mềm mới nhất từ đó. Trong trường hợp của chúng tôi, vì python3 đã được cài đặt nên nó sẽ không làm bất cứ điều gì như bạn có thể thấy bên dưới
cyberithub@ubuntu:~$ sudo apt install python3
Reading package lists.. Done
Building dependency tree
Reading state information.. Done
python3 is already the newest version [3.8.2-0ubuntu2].
The following packages were automatically installed and are no longer required:
libjs-jquery-ui pypy-lib python-matplotlib-data python3-cycler python3-kiwisolver
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Bước 3. Cài đặt pip3
Vì Selenium webdriver sẽ được cài đặt thông qua tiện ích pip3 nên điều quan trọng là phải cài đặt tiện ích này trước bằng cách sử dụng lệnh
cyberithub@ubuntu:~$ sudo apt install python3-pip Reading package lists.. Done Building dependency tree Reading state information.. Done The following packages were automatically installed and are no longer required: libjs-jquery-ui pypy-lib python-matplotlib-data python3-cycler python3-kiwisolver Use 'sudo apt autoremove' to remove them. The following NEW packages will be installed: python3-pip 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 231 kB of archives. After this operation, 1,050 kB of additional disk space will be used. Get:1 //in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 python3-pip all 20.0.2-5ubuntu1.6 [231 kB] Fetched 231 kB in 11s [21.6 kB/s] Selecting previously unselected package python3-pip. [Reading database .. 258471 files and directories currently installed.] Preparing to unpack .../python3-pip_20.0.2-5ubuntu1.6_all.deb ... Unpacking python3-pip [20.0.2-5ubuntu1.6] ... Setting up python3-pip [20.0.2-5ubuntu1.6] ... Processing triggers for man-db [2.9.1-1] ...2 như hình bên dưới. Nếu nó đã được cài đặt và có sẵn, thì bạn có thể trực tiếp tiếp tục và cài đặt Selenium webdriver bằng bước tiếp theo
quảng cáo
cyberithub@ubuntu:~$ sudo apt install python3-pip Reading package lists.. Done Building dependency tree Reading state information.. Done The following packages were automatically installed and are no longer required: libjs-jquery-ui pypy-lib python-matplotlib-data python3-cycler python3-kiwisolver Use 'sudo apt autoremove' to remove them. The following NEW packages will be installed: python3-pip 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 231 kB of archives. After this operation, 1,050 kB of additional disk space will be used. Get:1 //in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 python3-pip all 20.0.2-5ubuntu1.6 [231 kB] Fetched 231 kB in 11s [21.6 kB/s] Selecting previously unselected package python3-pip. [Reading database .. 258471 files and directories currently installed.] Preparing to unpack .../python3-pip_20.0.2-5ubuntu1.6_all.deb ... Unpacking python3-pip [20.0.2-5ubuntu1.6] ... Setting up python3-pip [20.0.2-5ubuntu1.6] ... Processing triggers for man-db [2.9.1-1] ...
Bước 4. Cài đặt thư viện Selenium
Bây giờ bạn có thể sử dụng gói python pip3 để cài đặt Selenium webdriver bằng cách sử dụng lệnh
cyberithub@ubuntu:~$ sudo apt install python3-pip Reading package lists.. Done Building dependency tree Reading state information.. Done The following packages were automatically installed and are no longer required: libjs-jquery-ui pypy-lib python-matplotlib-data python3-cycler python3-kiwisolver Use 'sudo apt autoremove' to remove them. The following NEW packages will be installed: python3-pip 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 231 kB of archives. After this operation, 1,050 kB of additional disk space will be used. Get:1 //in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 python3-pip all 20.0.2-5ubuntu1.6 [231 kB] Fetched 231 kB in 11s [21.6 kB/s] Selecting previously unselected package python3-pip. [Reading database .. 258471 files and directories currently installed.] Preparing to unpack .../python3-pip_20.0.2-5ubuntu1.6_all.deb ... Unpacking python3-pip [20.0.2-5ubuntu1.6] ... Setting up python3-pip [20.0.2-5ubuntu1.6] ... Processing triggers for man-db [2.9.1-1] ...3 như hình bên dưới
cyberithub@ubuntu:~$ sudo pip3 install selenium
Collecting selenium
Downloading selenium-4.5.0-py3-none-any.whl [995 kB]
|████████████████████████████████| 995 kB 3.1 MB/s
Collecting urllib3[socks]~=1.26
Downloading urllib3-1.26.12-py2.py3-none-any.whl [140 kB]
|████████████████████████████████| 140 kB 11.6 MB/s
Collecting trio-websocket~=0.9
Downloading trio_websocket-0.9.2-py3-none-any.whl [16 kB]
Collecting trio~=0.17
Downloading trio-0.22.0-py3-none-any.whl [384 kB]
|████████████████████████████████| 384 kB 3.1 MB/s
Collecting certifi>=2021.10.8
Downloading certifi-2022.9.24-py3-none-any.whl [161 kB]
|████████████████████████████████| 161 kB 3.2 MB/s
Collecting PySocks!=1.5.7,=1.5.6; extra == "socks"
Downloading PySocks-1.7.1-py3-none-any.whl [16 kB]
Collecting async-generator>=1.10
Downloading async_generator-1.10-py3-none-any.whl [18 kB]
Collecting wsproto>=0.14
Downloading wsproto-1.2.0-py3-none-any.whl [24 kB]
Requirement already satisfied: attrs>=19.2.0 in /usr/lib/python3/dist-packages [from trio~=0.17->selenium] [19.3.0]
Collecting outcome
..........................................................
Bước 5. Cài đặt Chrome WebDriver
Tùy theo trình duyệt mà bạn có thể chọn cài đặt Webdriver. Trong trường hợp của chúng tôi, chúng tôi đang cài đặt webdriver cho trình duyệt
cyberithub@ubuntu:~$ sudo apt install python3-pip Reading package lists.. Done Building dependency tree Reading state information.. Done The following packages were automatically installed and are no longer required: libjs-jquery-ui pypy-lib python-matplotlib-data python3-cycler python3-kiwisolver Use 'sudo apt autoremove' to remove them. The following NEW packages will be installed: python3-pip 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 231 kB of archives. After this operation, 1,050 kB of additional disk space will be used. Get:1 //in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 python3-pip all 20.0.2-5ubuntu1.6 [231 kB] Fetched 231 kB in 11s [21.6 kB/s] Selecting previously unselected package python3-pip. [Reading database .. 258471 files and directories currently installed.] Preparing to unpack .../python3-pip_20.0.2-5ubuntu1.6_all.deb ... Unpacking python3-pip [20.0.2-5ubuntu1.6] ... Setting up python3-pip [20.0.2-5ubuntu1.6] ... Processing triggers for man-db [2.9.1-1] ...4 bằng cách sử dụng lệnh
cyberithub@ubuntu:~$ sudo apt install python3-pip Reading package lists.. Done Building dependency tree Reading state information.. Done The following packages were automatically installed and are no longer required: libjs-jquery-ui pypy-lib python-matplotlib-data python3-cycler python3-kiwisolver Use 'sudo apt autoremove' to remove them. The following NEW packages will be installed: python3-pip 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 231 kB of archives. After this operation, 1,050 kB of additional disk space will be used. Get:1 //in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 python3-pip all 20.0.2-5ubuntu1.6 [231 kB] Fetched 231 kB in 11s [21.6 kB/s] Selecting previously unselected package python3-pip. [Reading database .. 258471 files and directories currently installed.] Preparing to unpack .../python3-pip_20.0.2-5ubuntu1.6_all.deb ... Unpacking python3-pip [20.0.2-5ubuntu1.6] ... Setting up python3-pip [20.0.2-5ubuntu1.6] ... Processing triggers for man-db [2.9.1-1] ...5 như hình bên dưới
cyberithub@ubuntu:~$ sudo apt install chromium-chromedriver [sudo] password for cyberithub: Reading package lists.. Done Building dependency tree Reading state information.. Done The following packages were automatically installed and are no longer required: libjs-jquery-ui pypy-lib python-matplotlib-data python3-cycler python3-kiwisolver Use 'sudo apt autoremove' to remove them. The following additional packages will be installed: chromium-browser The following NEW packages will be installed: chromium-browser chromium-chromedriver 0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded. Need to get 50.8 kB of archives. After this operation, 242 kB of additional disk space will be used. Do you want to continue? [Y/n] Y ....................................................
Bước 6. Kiểm tra trình điều khiển web Chrome
Sau khi webdriver được cài đặt thành công, bạn có thể kiểm tra bằng cách sử dụng lệnh
cyberithub@ubuntu:~$ sudo apt install python3-pip Reading package lists.. Done Building dependency tree Reading state information.. Done The following packages were automatically installed and are no longer required: libjs-jquery-ui pypy-lib python-matplotlib-data python3-cycler python3-kiwisolver Use 'sudo apt autoremove' to remove them. The following NEW packages will be installed: python3-pip 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 231 kB of archives. After this operation, 1,050 kB of additional disk space will be used. Get:1 //in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 python3-pip all 20.0.2-5ubuntu1.6 [231 kB] Fetched 231 kB in 11s [21.6 kB/s] Selecting previously unselected package python3-pip. [Reading database .. 258471 files and directories currently installed.] Preparing to unpack .../python3-pip_20.0.2-5ubuntu1.6_all.deb ... Unpacking python3-pip [20.0.2-5ubuntu1.6] ... Setting up python3-pip [20.0.2-5ubuntu1.6] ... Processing triggers for man-db [2.9.1-1] ...6 như hình bên dưới
cyberithub@ubuntu:~$ sudo apt install python3-pip Reading package lists.. Done Building dependency tree Reading state information.. Done The following packages were automatically installed and are no longer required: libjs-jquery-ui pypy-lib python-matplotlib-data python3-cycler python3-kiwisolver Use 'sudo apt autoremove' to remove them. The following NEW packages will be installed: python3-pip 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 231 kB of archives. After this operation, 1,050 kB of additional disk space will be used. Get:1 //in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 python3-pip all 20.0.2-5ubuntu1.6 [231 kB] Fetched 231 kB in 11s [21.6 kB/s] Selecting previously unselected package python3-pip. [Reading database .. 258471 files and directories currently installed.] Preparing to unpack .../python3-pip_20.0.2-5ubuntu1.6_all.deb ... Unpacking python3-pip [20.0.2-5ubuntu1.6] ... Setting up python3-pip [20.0.2-5ubuntu1.6] ... Processing triggers for man-db [2.9.1-1] ...1
Bước 7. Nâng cấp Selenium
Nếu bạn đã cài đặt Selenium, thì hãy nâng cấp nó lên phiên bản mới nhất bằng cách sử dụng lệnh
cyberithub@ubuntu:~$ sudo apt install python3-pip Reading package lists.. Done Building dependency tree Reading state information.. Done The following packages were automatically installed and are no longer required: libjs-jquery-ui pypy-lib python-matplotlib-data python3-cycler python3-kiwisolver Use 'sudo apt autoremove' to remove them. The following NEW packages will be installed: python3-pip 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 231 kB of archives. After this operation, 1,050 kB of additional disk space will be used. Get:1 //in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 python3-pip all 20.0.2-5ubuntu1.6 [231 kB] Fetched 231 kB in 11s [21.6 kB/s] Selecting previously unselected package python3-pip. [Reading database .. 258471 files and directories currently installed.] Preparing to unpack .../python3-pip_20.0.2-5ubuntu1.6_all.deb ... Unpacking python3-pip [20.0.2-5ubuntu1.6] ... Setting up python3-pip [20.0.2-5ubuntu1.6] ... Processing triggers for man-db [2.9.1-1] ...7 như hình bên dưới
cyberithub@ubuntu:~$ sudo apt install python3-pip Reading package lists.. Done Building dependency tree Reading state information.. Done The following packages were automatically installed and are no longer required: libjs-jquery-ui pypy-lib python-matplotlib-data python3-cycler python3-kiwisolver Use 'sudo apt autoremove' to remove them. The following NEW packages will be installed: python3-pip 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 231 kB of archives. After this operation, 1,050 kB of additional disk space will be used. Get:1 //in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 python3-pip all 20.0.2-5ubuntu1.6 [231 kB] Fetched 231 kB in 11s [21.6 kB/s] Selecting previously unselected package python3-pip. [Reading database .. 258471 files and directories currently installed.] Preparing to unpack .../python3-pip_20.0.2-5ubuntu1.6_all.deb ... Unpacking python3-pip [20.0.2-5ubuntu1.6] ... Setting up python3-pip [20.0.2-5ubuntu1.6] ... Processing triggers for man-db [2.9.1-1] ...3
Bước 8. Gỡ cài đặt Selenium
Khi bạn đã hoàn tất với selen, bạn có thể chọn gỡ cài đặt nó khỏi Hệ thống của mình bằng cách sử dụng lệnh
cyberithub@ubuntu:~$ sudo apt install python3-pip Reading package lists.. Done Building dependency tree Reading state information.. Done The following packages were automatically installed and are no longer required: libjs-jquery-ui pypy-lib python-matplotlib-data python3-cycler python3-kiwisolver Use 'sudo apt autoremove' to remove them. The following NEW packages will be installed: python3-pip 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 231 kB of archives. After this operation, 1,050 kB of additional disk space will be used. Get:1 //in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 python3-pip all 20.0.2-5ubuntu1.6 [231 kB] Fetched 231 kB in 11s [21.6 kB/s] Selecting previously unselected package python3-pip. [Reading database .. 258471 files and directories currently installed.] Preparing to unpack .../python3-pip_20.0.2-5ubuntu1.6_all.deb ... Unpacking python3-pip [20.0.2-5ubuntu1.6] ... Setting up python3-pip [20.0.2-5ubuntu1.6] ... Processing triggers for man-db [2.9.1-1] ...8 như hình bên dưới