python - 如何删除 CSS 加载和 Selenium 文本
问题描述
伙计们,如何让我的 Selenium 脚本更快。我有这个 Selenium 脚本:
import time
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by
import By from selenium.webdriver.common.keys import Keys
from xvfbwrapper import Xvfb
display = Xvfb()
display.start()
option = Options()
prefs = {'profile.default_content_setting_values': {'images': 2}}
option.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(options = option)
driver.get("https://www.nike.com.br/chuteira-nike-premier-2-sala-unissex-153-169-171-309321")
wait = WebDriverWait(driver, 5)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.cc-allow'))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//label[@for='tamanho__idM40F395']"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button#anchor-acessar-unite-oauth2'))).click()
伙计们,问题是这个脚本很慢。如何在这种情况下只等待时间而不是等待比在这种情况下单击元素的时间更长?因为我认为由于这个摘录wait = WebDriverWait (driver, 5)
页面期望更多的点击元素,所以我只是想在这个场景中等待时间,并且还注意到在这个脚本中我承担了图像的负载,我做了这是为了优化脚本,是否可以删除图像和文本的加载? 我想删除一些 html 和 css 文本元素以加快加载速度。有人可以告诉我该怎么做吗?
解决方案
我做了一些研究,以下是我的结果。您有几个选择可以尝试:
1 无头模式:
相信无头模式更快。检查这个答案Running Selenium with Headless Chrome Webdriver和这里的无头浏览器自动化差异 您的代码看起来像(加载图片未禁用):
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
start_time = time.time()
options = webdriver.ChromeOptions()
options.headless = True # running in headless mode
options.add_argument("--disable-blink-features")
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(chrome_options=options, executable_path='/snap/bin/chromium.chromedriver')
driver.get("https://www.nike.com.br/chuteira-nike-premier-2-sala-unissex-153-169-171-309321")
wait = WebDriverWait(driver, 15)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.cc-allow'))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//label[@for='tamanho__idM40F395']"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button#anchor-acessar-unite-oauth2'))).click()
end_time = time.time()
print("Total execution time: {} seconds".format(end_time - start_time))
总执行时间(3 次运行):
12.759594202041626 seconds
13.571481466293335 seconds
17.36514186859131 seconds
2 禁用图片 参考:Python: Disable images in Selenium Google ChromeDriver
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
start_time = time.time()
options = webdriver.ChromeOptions()
prefs = {'profile.default_content_setting_values': {'images': 2}}
options.add_experimental_option("prefs", prefs) # disable loading pictures
options.add_argument("--disable-blink-features")
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(chrome_options=options, executable_path='/snap/bin/chromium.chromedriver')
driver.get("https://www.nike.com.br/chuteira-nike-premier-2-sala-unissex-153-169-171-309321")
wait = WebDriverWait(driver, 15)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.cc-allow'))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//label[@for='tamanho__idM40F395']"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button#anchor-acessar-unite-oauth2'))).click()
end_time = time.time()
print("Total execution time: {} seconds".format(end_time - start_time))
Total execution time: 10.57245922088623 seconds
Total execution time: 13.770843982696533 seconds
Total execution time: 10.908315896987915 seconds
3 Disable CSS Reference How to disable CSS in Python selenium using ChromeOptions - 现在不确定这个选项是否正确,因为它不会缩短等待时间并且只能在无头模式下工作:
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
start_time = time.time()
options = webdriver.ChromeOptions()
prefs = {'profile.default_content_setting_values': {'cookies': 2, 'images': 2, 'javascript': 2,
'plugins': 2, 'popups': 2, 'geolocation': 2,
'notifications': 2, 'auto_select_certificate': 2, 'fullscreen': 2,
'mouselock': 2, 'mixed_script': 2, 'media_stream': 2,
'media_stream_mic': 2, 'media_stream_camera': 2, 'protocol_handlers': 2,
'ppapi_broker': 2, 'automatic_downloads': 2, 'midi_sysex': 2,
'push_messaging': 2, 'ssl_cert_decisions': 2, 'metro_switch_to_desktop': 2,
'protected_media_identifier': 2, 'app_banner': 2, 'site_engagement': 2,
'durable_storage': 2}}
options.add_experimental_option("prefs", prefs) # disable loading pictures
options.headless = True # running in headless mode
options.add_argument("--disable-blink-features")
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(chrome_options=options, executable_path='/snap/bin/chromium.chromedriver')
driver.get("https://www.nike.com.br/chuteira-nike-premier-2-sala-unissex-153-169-171-309321")
wait = WebDriverWait(driver, 15)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.cc-allow'))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//label[@for='tamanho__idM40F395']"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button#anchor-acessar-unite-oauth2'))).click()
end_time = time.time()
print("Total execution time: {} seconds".format(end_time - start_time))
Total execution time: 13.649555444717407 seconds
Total execution time: 12.698347806930542 seconds
Total execution time: 13.572114706039429 seconds
结论
从我看到的使用prefs = {'profile.default_content_setting_values': {'images': 2}}
是最佳解决方案,至少对于您的情况。无头模式不会增加速度的任何改进。
但是,根据 Selenium 代码和其他因素,结果可能会有所不同。
此外,在这里您可以找到一些关于如何加快测试速度的好建议。列表:
- 使用快速选择器
- 使用更少的定位器
- 创建原子测试
- 不要两次测试相同的功能
- 仅使用显式等待
- 使用 chrome 驱动程序
- 使用无头浏览器的驱动程序
- 重用浏览器实例
- 并行运行脚本
- 使用 HTTP 解析库
- 预填充 cookie 不会在网页中加载图像
您没有使用隐式等待,而是使用更快的 css 选择器。
推荐阅读
- java - “三角形 1 是抽象的;无法实例化”驱动程序错误
- angular - 如何在 Angular 中实现 Firebase 分布式计数器扩展
- android - React Native Android 构建失败
- asp.net-core - 使用带有 DockerFile 的 Azure DevOps 设置网络核心应用程序的程序集版本
- visual-studio - Visual Studio 扩展 - 获取对 Git 历史记录中当前选定项目的引用
- flutter - Flutter:集成测试。是否可以可视化点击和滚动?
- c - variable definition where function is called multiple times to read a file
- c - 关于大型原子操作的编译器警告
- spring-boot - Spring Security - 获取登录页面时,安全性尝试进行身份验证并返回 401 错误
- git - Github 上的贡献没有出现