简体   繁体   English

如何避免此错误“SyntaxError:解析时意外 EOF”

[英]How can I avoid this error "SyntaxError: unexpected EOF while parsing"

while True:
    try:
        result = login(browser, url)
        if not result != 0:
            login(browser, url)
            while True:
                try:
                    time.sleep(1)
                    browser.find_element_by_class_name("submit").click()
                except:
                    browser.find_element_by_xpath("//a[contains(.,'Restart Battle')]").click()

It shows error on this line:它在这一行显示错误:

browser.find_element_by_xpath("//a[contains(.,'Restart Battle')]").click()

I tried removing parentheses and spaces and other stuff, but it isn't going off.我尝试删除括号和空格以及其他内容,但它并没有消失。

Any idea what I should do?知道我应该怎么做吗?

When using try, you need an except.使用 try 时,需要一个 except。 For example, the following code prints error :例如,以下代码打印error

try:
    x=1/0
except:
    print("error")

But this throws an SyntaxError: unexpected EOF while parsing :但这会引发SyntaxError: unexpected EOF while parsing

try:
    x=1/0
# except:
#   print("error")

So, you need an exception at the end of the try statement here:因此,您需要在此处的 try 语句末尾添加一个异常:

while True:
    try:
        result = login(browser, url)
        if not result != 0:
        ...
    except:
        ...

This error means that in your script something begins , but the EOF (End of File — the file is your script) occurred before it ends.此错误意味着在您的脚本中开始了某些内容,但 EOF(文件结束 - 文件是您的脚本)在它结束之前发生。

(Similar as the unexpected “The End” in a watched crime thriller without revealing a perpetrator.) (类似于在没有揭露肇事者的情况下观看的犯罪惊悚片中意想不到的“结局”。)

It means that you started something (eg writing " as a start of the string literal), but you never ended it (no closing " ).这意味着您开始了某些事情(例如,将"作为字符串文字的开头写入),但您从未结束它(没有结束" )。

The typical cases are unbalanced quotes, apostrophes, parentheses, and so on.典型的例子是不平衡的引号、撇号、括号等。

In your case it is the first try: without the expected except: just in the 2 nd line of your script.在您的情况下,这是第一次try:没有预期的except:仅在脚本的第二行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM