尝试,除非继续Python不工作

2024-05-12 15:29:20 发布

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

我尝试以下例行程序:

    While true:
    print("back to beginning")  
    self.driver.get("http://mylovelywebsite.com")
        try:
            wait.until(
                EC.title_is("This is my title")
            )
        except TimeoutException as ex:
            print("HERE!")
            print(ex.message)
            self.driver.quit()
            continue

它是为了硒,它只是在等着看标题是否在那里。但是,我相信这只是python的问题(selenium的工作很好)

问题是ex.message公司不打印任何内容。但是,即使我删除它,它也不会转到.quit()函数,当它到达continue语句时,它只返回print(“HERE!”)语句(而不是回到脚本的开头。在

我想知道如何使它在出现错误时回到脚本的开头并再次运行?是否必须将continue and quit()1缩进更少?但我不确定这是否有效,因为即使没有捕捉到错误,它也会到达continue语句。在


Tags: self脚本messageheretitleisdriver错误
1条回答
网友
1楼 · 发布于 2024-05-12 15:29:20
  1. 你的压痕掉了。在
  2. While应该是while
  3. true应该是{}
  4. TimeoutException没有message属性;请改用str(ex)。在
  5. 您尚未显示是否已导入TimeoutException。使用from selenium.common.exceptions import TimeoutException

正确的格式

from selenium.common.exceptions import TimeoutException

while True:
    print("back to beginning")  
    self.driver.get("http://mylovelywebsite.com")
    try:
        wait.until(EC.title_is("This is my title"))
    except TimeoutException as ex:
        print("HERE!")
        print(str(ex))
        self.driver.quit()
        continue

你所描述的程序

^{pr2}$

无限循环;以ctrl+c终止。在

相关问题 更多 >