Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error loop with Chatgpt
#1
Hi all, sorry in advance for the "dumb" question, but I was trying to create a simple script for web scraping contacts from government sites.
I have asked for help in touching up and cleaning up the chatgpt script, but unfortunately it doesn't lead to a result at all. We keep going in circles talking about bad indentation and bad syntax. But even though I have tried about 30 different scripts, it still throws the same error.
Could any of you take a look at this and advise what to do?

Thanks a lot for any help

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

# Automatické stažení správného ChromeDriveru
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

# URL stránky s kontaktními informacemi
url = "https://www.bayernportal.de/dokumente/behoerde/40997963439"
driver.get(url)

# Čekání na načtení vyskakovacího okna pro cookies a jeho zavření
try:
    wait = WebDriverWait(driver, 20)
    # Opravený XPath pro tlačítko "Alle cookies akzeptieren"
    cookie_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Alle cookies akzeptieren')]")))
    cookie_button.click()  # Kliknutí na souhlas s cookies
    print("Cookies byly potvrzeny.")
except Exception as e:
    print(f"Vyskakovací okno pro cookies nebylo nalezeno: {e}")
    # Pokusíme se použít JavaScript k odkliknutí tlačítka, pokud není k dispozici
    try:
        script = "document.querySelector('button').click();"
        driver.execute_script(script)
        print("Cookies byly potvrzeny (JavaScript).")
    except Exception as js_error:
        print(f"Chyba při klikání pomocí JavaScriptu: {js_error}")

# Čekání na načtení požadovaných elementů (telefon a e-mail)
wait = WebDriverWait(driver, 20)

# Extrahování telefonního čísla
try:
    phone_element = wait.until(EC.presence_of_element_located((By.XPATH, "//a[starts-with(@href, 'tel:')]")))
    phone_number = phone_element.text.strip() if phone_element else "Nenalezeno"
    print("Telefon:", phone_number)
except Exception as e:
    print(f"Chyba při získávání telefonního čísla: {e}")
    phone_number = "Nenalezeno"

# Extrahování e-mailu
try:
    email_element = wait.until(EC.presence_of_element_located((By.XPATH, "//a[contains(@href, 'mailto:')]")))
    email_address = email_element.text.strip() if email_element else "Nenalezeno"
    print("Email:", email_address)
except Exception as e:
    print(f"Chyba při získávání e-mailu: {e}")
    email_address = "Nenalezeno"

# Zavření prohlížeče
driver.quit()
And here is firther explanation from chatgpt: The issue you are facing is still related to the indentation and the presence of unnecessary escape characters (\) in your code. In Python, indentation must be consistent, and escape characters are not required in the syntax you are using.

Let's go through a few key fixes:

Key Problems:
Indentation Errors: Python requires consistent indentation. Each nested block (like the code in try and except) must be indented the same way. Inconsistent indentation causes the IndentationError.
Escape Characters: You have extra \ (backslash) characters in places where they are not needed. These can cause syntax issues.
Corrected Code:
Here is the corrected version of your code without the indentation issues and escape characters

What has been fixed:
Removed Escape Characters: I removed unnecessary backslashes (\) in places like the XPath queries and string literals.
Corrected Indentation: Ensured consistent indentation throughout the code. Every block inside try, except, and other structures has been indented correctly.
No additional characters: I removed extra backslashes at the end of lines.
This code should now run without errors. If the problem persists or if there are specific exceptions that need attention, feel free to share those details.
Reply
#2
I took a look at your script, and the good news is that there aren’t actually any indentation problems or stray backslashes in the version you posted — it’s already clean Python. If you’re still seeing errors, the issue is most likely something else, such as the site’s cookie button not matching the XPath you used, or ChromeDriver not matching your Chrome version.

A few things to check:

Cookie popup selector
The original XPath

//button[contains(text(), 'Alle cookies akzeptieren')]


may not match exactly what’s in the page (sometimes the text is wrapped in spans or slightly different). A broader XPath like this tends to work better:

//button[contains(., 'Alle')]


Dynamic content
Parts of BayernPortal load late or after scrolling. If you don’t find phone/email right away, scroll down before searching:

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")


ChromeDriver version
If you see errors like “session not created: This version of ChromeDriver only supports Chrome version XX”, it means your Chrome and ChromeDriver versions are out of sync. Update Chrome, or pin a specific driver version in ChromeDriverManager.

Headless mode
If you’re running on a server without a GUI, add:

options = webdriver.ChromeOptions()
options.add_argument("--headless=new")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
Here’s a minimal working version you can try out:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

options = webdriver.ChromeOptions()
# options.add_argument("--headless=new")  # uncomment if no GUI available
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)

url = "https://www.bayernportal.de/dokumente/behoerde/40997963439"
driver.get(url)

wait = WebDriverWait(driver, 20)

# Try to accept cookies
try:
    cookie_button = wait.until(
        EC.element_to_be_clickable((By.XPATH, "//button[contains(., 'Alle')]"))
    )
    cookie_button.click()
    print("✅ Cookies accepted.")
except Exception as e:
    print("⚠️ No cookie popup:", e)

# Get phone number
try:
    phone = wait.until(
        EC.presence_of_element_located((By.XPATH, "//a[starts-with(@href, 'tel:')]"))
    ).text.strip()
except:
    phone = "Nenalezeno"

# Get email
try:
    email = wait.until(
        EC.presence_of_element_located((By.XPATH, "//a[starts-with(@href, 'mailto:')]"))
    ).text.strip()
except:
    email = "Nenalezeno"

print("Telefon:", phone)
print("Email:", email)

driver.quit()
Larz60+ write Aug-26-2025, 11:36 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Tags added for you this time. Please use BBCode tags on future posts.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm trying to make my Alexa/Echo dot 3 powered by ChatGPT.... mariozio 1 2,296 Apr-20-2023, 05:24 PM
Last Post: farshid
  Com Error with macro within for loop due to creating new workbook in the loop Lastwizzle 0 2,269 May-18-2019, 09:29 PM
Last Post: Lastwizzle

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020