2016-01-06 75 views
0

我想抓取whoscored,其中有很多的东西,并需要大量的时间来完全加载。然而,我想要的东西加载得非常快,但仍然执行driver.get(url),直到网页完全加载。 有什么办法可以防止这种情况发生?一旦我定义的某个元素出现在DOM中,就让get方法返回?我在考虑停止在浏览器中的作用。硒需要太多的时间加载一个页面

+0

相关:http://stackoverflow.com/q/22404581/771848。 – alecxe

回答

1

于是,我找到了这种行为,我想最好的解决办法是:

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") 

这将停止浏览器加载页面,你仍然可以抓取并与加载的网站进行互动。

+0

您也可以使用像AdBlock或NoScript这样的附加组件来过滤掉不需要加载的内容 – user2426679