几秒钟后如何停止html加载?

时间:2020-01-28 09:48:00

标签: python python-3.x selenium selenium-webdriver

我要检查一堆HTML,每次尝试使用driver.get(“ HTML”)加载它时,完成加载的时间都太长了。我的意思是,它已经在2秒内显示了我的需求,然后我认为大约10秒钟就可以加载东西。

我想我已经搜索了足够多的东西。这是我尝试过的东西。

  1. set_page_load_timeout 我用过

    driver.set_page_load_timeout(2)

    尝试: driver.get(“ HTML”) 除了TimeoutException: 通过

但是我发现,在获取TimeOutException之后,它不会重新激活它,因此驱动程序无法加载第二个HTML并直接进入TimeoutException

  1. pageLoadStrategy:

实际上,这可以工作。但不幸的是,我正在使用两个驱动程序。第一个驱动程序是收集HTML的驱动程序,第二个驱动程序是加载那些下载的HTML。

caps = DesiredCapabilities().CHROME
caps["pageLoadStrategy"] = "none"
normal = DesiredCapabilities().CHROME
normal["pageLoadStrategy"] = "normal"

driver2 = webdriver.Chrome("chromedriver78.exe", chrome_options=options2, desired_capabilities=caps)
driver = webdriver.Chrome("chromedriver78.exe", chrome_options=options, desired_capabilities = normal)

我发现的问题是仅应用了最后设置的一个“ desired_capabilities”。这意味着,驱动程序和驱动程序2共享最后的desirable_capabilities设置...因此,如果稍后设置驱动程序,则驱动程序2并没有什么区别,它仅加载HTML约10秒钟。如果以后再设置driver2,则整个驱动程序过程都将被破坏,因为它会在页面完全加载之前保持激活状态。

所以...这对我来说是更好的方法吗?希望我能有所帮助。

0 个答案:

没有答案