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")
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")
pageLoadStrategy Options
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!