1

I use PyCharm to right click Run 'hello ' or use shell to run python hello.py ,both return below error :

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

below code is copied from 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()

I used tab or four whitespaces before if name == "main",but both have no effect.

Venus
  • 1,184
  • 2
  • 13
  • 32

1 Answers1

4

The error is coming from the empty method above:

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

You can't have a completely empty method, the parser can't handle it. Add a pass statement to get it to parse:

def tearDown(self):
    pass
John Kugelman
  • 349,597
  • 67
  • 533
  • 578