Selenium Webdriver 等待 URL?

4 投票
1 回答
3320 浏览
提问于 2025-04-18 02:56

我有一个用Python写的Selenium Webdriver脚本,它可以打开一个网址;

def GotToURL(self):

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

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

问题是每15次中就有1次,这个网址加载不完。所以,我想加一个步骤,让它尝试运行

 print driver.current_url

10秒,如果还不行,就重启这个脚本。

有没有人有什么好主意?

我在osx上运行Selenium Webdriver和Python 2.7。

1 个回答

2

我能想到的最好办法是使用显式等待和隐式等待

比如,你可以这样定义驱动:

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

然后把你的操作放在一个try except语句里,指定一个TimeoutException。这样做的意思是,如果页面加载超过10秒,驱动就会抛出这个异常,然后你可以定义如何处理这种情况。

撰写回答