Bagaimana untuk Mempercepatkan Ujian Selenium dengan Menala Strategi Pemuatan Halaman?

Linda Hamilton
Lepaskan: 2024-11-14 20:30:03
asal
190 orang telah melayarinya

How to Speed Up Selenium Tests by Tuning Page Loading Strategy?

Tuning Selenium to Bypass Slow Script Loading

Selenium's default behavior is to wait until a page fully loads before proceeding, which can become problematic when pages contain slow or unreliable scripts. To mitigate this issue, consider adjusting Selenium's page loading strategy.

The pageLoadStrategy property allows you to manipulate how Selenium handles page load events. By specifying the appropriate strategy, you can limit the time Selenium waits, block AJAX requests, and even disable script loading entirely.

Configure pageLoadStrategy for Different Browsers

Firefox:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

caps = DesiredCapabilities().FIREFOX
caps["pageLoadStrategy"] = "normal"  # full page load
# caps["pageLoadStrategy"] = "eager"  # interactive
# caps["pageLoadStrategy"] = "none"
driver = webdriver.Firefox(desired_capabilities=caps, executable_path=r'C:\path\to\geckodriver.exe')
driver.get("http://google.com")
Salin selepas log masuk

Chrome:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

caps = DesiredCapabilities().CHROME
caps["pageLoadStrategy"] = "normal"  # full page load
# caps["pageLoadStrategy"] = "eager"  # interactive
# caps["pageLoadStrategy"] = "none"
driver = webdriver.Chrome(desired_capabilities=caps, executable_path=r'C:\path\to\chromedriver.exe')
driver.get("http://google.com")
Salin selepas log masuk

pageLoadStrategy Options

  • normal: Wait for the full page load, including scripts and AJAX requests.
  • eager: Wait until the page is interactive, allowing Selenium to continue execution while scripts and AJAX still load asynchronously.
  • none: Disable all script loading and AJAX requests, allowing Selenium to immediately proceed.

Note: The "eager" strategy is still under development for ChromeDriver implementations, so it may not be fully supported across all browsers.

Atas ialah kandungan terperinci Bagaimana untuk Mempercepatkan Ujian Selenium dengan Menala Strategi Pemuatan Halaman?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan