Selenium 加载源代码:禁用图片和 JavaScript
使用 Selenium 时,如何使页面只加载源代码不加载其他文件内容?要使页面只加载源代码而不加载其他文件,可以使用 Selenium 的 ChromeOptions 类中的 add_argument() 方法来设置禁用自动加载图片和禁用 JavaScript。以下是示例代码:
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument('--disable-infobars')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-plugins-discovery')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-images')
chrome_options.add_argument('--disable-javascript')
driver = webdriver.Chrome(chrome_options=chrome_options)
在上面的代码中,我们添加了以下选项:
- '--disable-images':禁用自动加载图片。
- '--disable-javascript':禁用 JavaScript。
这样页面只会加载源代码而不会加载其他文件。
原文地址: https://www.cveoy.top/t/topic/ooVi 著作权归作者所有。请勿转载和采集!