Python 如何删除CSS加载和Selenium文本
如何让我的Selenium脚本更快,伙计们。我有以下脚本:Python 如何删除CSS加载和Selenium文本,python,python-3.x,selenium,selenium-webdriver,selenium-chromedriver,Python,Python 3.x,Selenium,Selenium Webdriver,Selenium Chromedriver,如何让我的Selenium脚本更快,伙计们。我有以下脚本: import time from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.chr
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无头模式:
人们认为无头模式速度更快。检查此答案,然后单击此处
您的代码如下(未禁用加载图片):
总执行时间(3次运行):
2禁用图片
参考:
3禁用CSS
参考-不确定此选项现在是否正确,因为它不会改善等待时间,并且仅在无头模式下工作:
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}}
是最佳的解决方案,至少对于您的情况是这样。
无头模式不会提高速度
然而,根据硒代码和其他因素,结果可能会有所不同
此外,您还可以找到一些关于如何加快测试速度的好建议。
名单:
- 使用快速选择器
- 使用更少的定位器
- 创建原子测试
- 不要对同一功能进行两次测试
- 仅使用显式等待
- 使用chrome驱动程序
- 为无头浏览器使用驱动程序
- 重新使用浏览器实例
- 并行运行脚本
- 使用HTTP解析库
- 预填充Cookie不会在网页中加载图像
您没有使用隐式等待,而是使用更快的css选择器。您使用此选择器的目标是什么?我查看了页面上的所有标签元素,没有一个具有类似的
for
属性。此外,您已经为您的问题标记了python-3.x和python-2.7。您使用的是哪个版本?@C.Peck页面上有几十个标签元素附加了for
属性,但没有一个标签元素作为标签value@JD2775是的,这也是我所看到的。(或任何类似于该值的tamanho_uuuID40
)@JD2775我编辑了问题的URL,错误的是现在:)
12.759594202041626 seconds
13.571481466293335 seconds
17.36514186859131 seconds
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
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