栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

无法摆脱脚本中的硬编码延迟

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

无法摆脱脚本中的硬编码延迟

定义 超时 (将允许经过的指定时间段)以摆脱硬编码的延迟。超时需要进行硬编码。

此代码与原始代码之间的区别是:

  • 循环本身一遍又一遍地运行(每次迭代不等待6秒),并检查是否有新内容,直到找到新内容或达到超时为止。
  • 如果延迟加载花费的时间比预期的要长,例如在将数字21加载到50时,循环将“等待”并尝试获取超时中定义的最大时间的新内容。
  • 缺点:在加载所有内容的最后一步,循环将花费与设置超时相同的秒数。

码:

Sub Getlinks()    Dim driver As New ChromeDriver, prevlen&, curlen&    Dim posts As Object, post As Object    Dim timeout As Integer, startTime As Double    timeout = 10 ' set the timeout to 10 seconds    With driver        .get "http://fortune.com/fortune500/list/"        prevlen = .FindElementsByClass("company-title").Count        startTime = Timer ' set the initial starting time        Do .Executescript ("window.scrollTo(0, document.body.scrollHeight);") Set posts = .FindElementsByClass("company-title") curlen = posts.Count If curlen > prevlen Then     startTime = Timer ' reset start time if new elements found     prevlen = curlen ' set new prevlen End If        Loop While Round(Timer - startTime, 2) <= timeout ' check if timeout is reached        For Each post In posts R = R + 1: Cells(R, 1) = post.Text        Next post    End WithEnd Sub


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/374645.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号