2k1
  • Home
  • Programming
  • System
  • Design
  • Applications
  • Tech
No Result
View All Result
  • Login
2k1
  • Home
  • Programming
  • System
  • Design
  • Applications
  • Tech
No Result
View All Result
2k1
No Result
View All Result

Cách dùng selenium trong python

Nguyen Pham by Nguyen Pham
12/03/2021
in Programming, Python
Reading Time: 2 mins read
A A
9

Selenium là một khuôn khổ di động để thử nghiệm các ứng dụng web. Selenium cung cấp một công cụ phát lại để tạo các bài kiểm tra chức năng mà không cần phải học ngôn ngữ kịch bản kiểm tra.(wikipedia).

Để sử dụng selenium trong python chúng ta cần cài thư viên selenium

pip install selenium

Bây giờ sẽ là ví dụ mình dùng selenium tải ảnh từ facebook:

from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from selenium import *
import os
import time
import pyautogui
from selenium.webdriver import ActionChains

Đâu tiên chúng ta cần khởi tạo một driver để điều khiển trình duyệt:

options = webdriver.ChromeOptions()
options.add_argument("--disable-notifications")
driver = webdriver.Chrome("chromedriver.exe",options=options)

Để mở 1 trang web ta làm như sau:

driver.get("https://facebook.com")

chúng ta có thể thêm time.sleep(1) để chờ nó tải trang facebook rồi thực hiện các thao tác tiếp theo. Như thực hiện DOM

    driver.find_element_by_id("email").send_keys("[email protected]")
driver.find_element_by_id("pass").send_keys("*******")
driver.find_element_by_id("u_0_b").click()

Cách chạy mã javascript:

driver.execute_script('''
    document.querySelectorAll('[data-pagelet="root"')[3].getElementsByTagName('a')[0].click()
    document.querySelectorAll('[data-pagelet="root"')[3].getElementsByTagName('a')[0].getAttribute('href')''')

cách gửi 1 nút:

a = driver.find_element_by_tag_name('body')
a.send_keys(Keys.ARROW_RIGHT)

Cách lấy url hiện tại:

driver.current_url

Tags: pythonselenium
Previous Post

Cách đổi DNS trong Ubuntu

Next Post

Hướng dẫn cách cài vscode.

Related Posts

Xây dựng todo app với smartcontract
Go

Giới thiệu về đa luồng trong ngôn ngữ lập trình go có code minh họa.

by Nguyen Pham
29/06/2023
Programming

Lập trình rust cơ bản – vòng lặp

by Nguyen Pham
22/12/2022
Programming

Lập trình python cơ bản – Hello world

by Nguyen Pham
16/11/2022
Programming

Lập trình go cơ bản – Hello world

by Nguyen Pham
04/12/2022
Programming

Lập trình rust cơ bản – Biến

by Nguyen Pham
16/11/2022
Programming

Lập trình rust cơ bản – hello world

by Nguyen Pham
14/11/2022
Load More
Next Post

Hướng dẫn cách cài vscode.

Please login to join discussion
Stock

Phân tích mã cổ phiếu VCB

by Nguyen Pham
26/04/2025
0

Phân tích mã cổ phiếu VCB (Ngân hàng TMCP Ngoại thương Việt Nam - Vietcombank) 1. Tổng quan về Vietcombank...

Read more

Facebook, Instagram bất ngờ sập trên diện rộng, liên tục đăng xuất người dùng!

05/03/2024
Xây dựng todo app với smartcontract

Web3 là gì?

30/06/2023
Xây dựng todo app với smartcontract

Giới thiệu về đa luồng trong ngôn ngữ lập trình go có code minh họa.

29/06/2023

Chạy ứng dụng react native đầu tiên của bạn

29/06/2023

@2021 2k1.org [email protected]

No Result
View All Result
  • Home
  • Review
  • Applications
  • Computers
  • Gaming
  • Microsoft

© 2021 NData

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In