Hello im getting in this script an indentationError

time.sleep(3)
driver.find_element_by_id("mainMenuRoutes").click()
time.sleep(1)
time.sleep(2)
if driver.find_element_by_xpath('//*[@class="m-btn full-btn pos-btn" and @id="departAll"]').is_displayed():
    print("Routen werden gestartet")
    driver.find_element_by_xpath('//*[@class="m-btn full-btn pos-btn" and @id="departAll"]').click()
    time.sleep(2)
    driver.find_element_by_id("mainMenuRoutes").click
    time.sleep(2)
else:
    print("Es konnte nichts gestartet werden")

if kerosin_kaufen:
    driver.find_element_by_css_selector("#mainMenuFuel").click()
    time.sleep(2)
    KerosinCheckA = driver.find_element_by_id("pricePerLbsOut").text
    KerosinCheck = re.sub("\,","",KerosinCheckA)
    print (KerosinCheck)
    if KerosinCheck >= kerosin_max:
        print("Kerosin ist billig - wird gekauft!")
        driver.find_element_by_css_selector("#iAmount").send_keys(kerosin_billig)
        time.sleep(1)
        driver.find_element_by_css_selector("#fuelBtnPurchase").click()
    else:
        BestandA = driver.find_element_by_css_selector("#holdingOut").text
        Bestand = re.sub("\,","",BestandA)
        print(Bestand)
        if Bestand <= kerosin_min:
            print("zu wenig Kerosin - wird gekauft")
            driver.find_element_by_css_selector("#iAmount").send_keys(kerosin_leer)
            time.sleep(2)
            driver.find_element_by_css_selector("#fuelBtnPurchase").click()
            print("Gekauft!")
        else:           
else:
    print ("h")

at the last else I'm getting this error: else: ^ IndentationError: expected an indented block

I dont know what to do when i write an elif in between of those im getting this error in the elif. Hope you guys can help me

You seem to have "else" twice near the end of the code snippet.

On some other things that I would change:

  1. You call time.sleep() twice at the start of the code, one right just after the other
  2. Remove time.sleep() altogether and wait until a certain element is shown instead. You can either set implicit wait globally, which will affect all of the webdriver`s locator functions, or create a function with an explicit wait that will end when a certain element is available in the DOM. More about that here: https://stackoverflow.com/questions/27618695/when-to-use-implicit-wait-and-where-to-use
  3. You seem to mix locators a lot. It`s a good practice to use only one type of locator (id, xpath,...). Makes the code easier to maintain.
  • Thanks I made a time.sleep at the else: after print("Gekauft!") now its working – Laurenz Z Sep 13 at 14:04
  • 1
    @LaurenzZ dont hesitate to mark answers as correct once you issue has been resolved. – Alexey R. Sep 14 at 13:27

Python expects a statement after an else statement. The error is caused by the empty else just after print("Gekauft!"). The error message is a bit confusing though. When you remove this else the error will be resolved.

Imagine you have the following invalid code:

print("Check if water is boiling")
if temperature >= 100:
    print("Boiling")
else:
print("Not boiling") # Oops, forgot to indent this line.
print("Check finished")

While you meant to code this:

print("Check if water is boiling")
if temperature >= 100:
    print("Boiling")
else:
    print("Not boiling")
print("Check finished")

Then this makes clear why the compiler complains about expecting an indented block of code.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.