Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách kích hoạt chạy thử nghiệm headless trong Selenium với Python?

Selenium hỗ trợ chạy headless. Trên trình duyệt Chrome, chạy headless có thể triển khai với lớp ChromeOptions. Tạo đối tượng của lớp này và áp dụng phương thức add_argument. Cuối cùng, truyền tham số --headless cho phương thức này.

Hãy lấy tiêu đề "About Careers at Tutorials Point - Tutorialspoint" của trang được mở ở chế độ headless.

Cách kích hoạt chạy thử nghiệm headless trong Selenium với Python?

Ví dụ

Triển khai mã

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

# đối tượng Options class
c = Options()
# truyền tham số headless
c.add_argument("--headless")
# thêm headless vào webdriver object
driver = webdriver.Chrome(executable_path='../drivers/chromedriver', options=c)
# thời gian chờ ngầm
driver.implicitly_wait(5)
# mở url
driver.get("https://www.tutorialspoint.com/about/about_careers.htm")
print('Page title: ' + driver.title)
# thoát driver
driver.quit()

Kết quả

Cách kích hoạt chạy thử nghiệm headless trong Selenium với Python?