Selenium 加载页面的时间太长
Selenium takes too much time loading a page
我正在尝试抓取 whoscored,它包含很多内容并且需要很长时间才能完全加载。然而,我想要的东西加载得非常快,但是 driver.get(url) 仍然执行直到网页完全加载。
有什么办法可以防止这种情况发生吗?要在 DOM 中出现我定义的某些元素后立即使用 get 方法 return 吗?我在想 stop 在浏览器中的工作原理。
因此,对于我想要的那种行为,我找到的最佳解决方案是:
profile = webdriver.FirefoxProfile()
profile.set_preference("webdriver.load.strategy", "unstable")
profile.update_preferences()
driver = webdriver.Firefox(firefox_profile=profile)
driver.get(url)
try:
wait = WebDriverWait(driver, timeout=20, poll_frequency=0.1)
wait.until(<expectation object>)
finally:
driver.execute_script("return window.stop")
这将停止浏览器加载页面,您仍然可以抓取已加载的网站并与之交互。
我正在尝试抓取 whoscored,它包含很多内容并且需要很长时间才能完全加载。然而,我想要的东西加载得非常快,但是 driver.get(url) 仍然执行直到网页完全加载。 有什么办法可以防止这种情况发生吗?要在 DOM 中出现我定义的某些元素后立即使用 get 方法 return 吗?我在想 stop 在浏览器中的工作原理。
因此,对于我想要的那种行为,我找到的最佳解决方案是:
profile = webdriver.FirefoxProfile()
profile.set_preference("webdriver.load.strategy", "unstable")
profile.update_preferences()
driver = webdriver.Firefox(firefox_profile=profile)
driver.get(url)
try:
wait = WebDriverWait(driver, timeout=20, poll_frequency=0.1)
wait.until(<expectation object>)
finally:
driver.execute_script("return window.stop")
这将停止浏览器加载页面,您仍然可以抓取已加载的网站并与之交互。