Selenium Webdriver等待url?

2024-10-01 15:36:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Selenium Webdriver脚本,用python编写,可以打开一个url

def GotToURL(self):

  #Go to url
  driver.get("http://someurl.com")

  #Prints url once it is finished loading
  print driver.current_url

问题是每15个url中就有1个无法完成加载。所以,我只想添加一个尝试运行该步骤的步骤

^{pr2}$

持续10秒,如果不起作用,重新启动脚本。在

有什么想法吗?在

在osx上运行Selenium Webdriver和python2.7


Tags: toself脚本comhttpurlgoget
1条回答
网友
1楼 · 发布于 2024-10-01 15:36:17

我能想到的最好的选择是使用Explicit and Implicit Waits。在

例如,可以这样定义驱动程序

driver = webdriver.Ie() #note that I'm using IE
driver.set_page_load_timeout(10) #set the max to 10s

然后将操作放入try except语句中,指定TimeoutException。这样做是说,如果页面加载时间超过10秒,驱动程序必须抛出异常,然后您可以定义如何处理这种情况。在

相关问题 更多 >

    热门问题