python if __name__ ==“ __main__”:IndentationError:预期为缩进块

金星

我使用PyCharm右键单击Run'hello'或使用shell运行python hello.py,都返回以下错误:

    if __name__ == "__main__":
                             ^ 
IndentationError: expected an indented block

下面的代码是从https://selenium-python.readthedocs.io/getting-started.html复制的

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys


class TestHello(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome(
            "D:\\Softwares\\selenium\\ChromeDriver 83.0.4103.39\\chromedriver_win32\\chromedriver.exe")

    def test_pick_goods(self):
        driver = self.driver
        driver.get("http://www.python.org")
        self.assertIn("Python", driver.title)
        elem = driver.find_element_by_name("q")
        elem.send_keys("pycon")
        elem.send_keys(Keys.RETURN)
        assert "No results found." not in driver.page_source
        print("-----last line----")

    def tearDown(self):
        #self.driver.close()


if __name__ == "__main__":
    unittest.main()

如果name ==“ main ”,我使用了制表符或四个空格,但两者都无效。

约翰·库格曼

错误来自上面的空方法:

def tearDown(self):
    #self.driver.close()

您不能有一个完全空的方法,解析器无法处理它。添加一条pass语句以使其解析:

def tearDown(self):
    pass

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

IndentationError:预期为缩进的块[Python]

使用python-multiprocessing与if __name__ =='__main__'相关的谜

将多个参数传递给 Python __name__=="__main__"

Pycharm“Python 控制台”中的 exec(open) 函数不执行“if __name__ == '__main__”块

if __name__ ==“ __main__”的位置:

替代 if __name__ == '__main__':

使用IF __name__ ==“ __main__”语句从另一个python脚本运行python脚本

是否有C与Python等效:如果__name__ =='__main__':在文件顶部写入main()?

TCL等同于Python的`if __name__ ==“ __main __”`

Python:如果__name__ =='__main__',则导入并初始化Argparse?

Python:如果__name__ =='__main__',则导入并初始化Argparse?

在Python中调用getLogger之前,是否需要显式检查__name__ ==“ __main__”?

在Python multiprocessing.Process中,我们是否必须使用`__name__ == __main__`?

如果__name__ =='__main__',则相当于python的node.js

Python:命令行,sys.argv,“如果__name__ =='__main__'”

__name__ ==“ __main__”怎么办?

来自 if __name__ == "__main__" 的返回语句

如果__name__ =='__main__'函数调用

如何使用 if __name__ == '__main__':

如何解决以下Python代码的错误错误:IndentationError:预期为缩进块

python manage.py shell <scripts / myscript.py在文件包含__name__ =='__main__'时不起作用

如何从另一个python文件中的`if __name__ ==“ __main __”`中执行代码

Python类变量名称与__name__

__name__ 与 __package__ 在 python 中

如果__name__ =='__main__'不起作用ipython

如果__name__ ==“ __main__”条件(带烧瓶/弹性豆茎)

如果在IPython中__name__ =='__main__'

如何测试或模拟“如果__name__ =='__main__'”内容

如何在“if __name__ == '__main__'”中模拟.patch 变量