[Python] JS seems not to work in Python / Selenium / Google Chrome - page is not loading

Discussão em 'Python' iniciado por Stack, Maio 23, 2025.

  1. Stack

    Stack Membro Participativo

    I would like to automate the process of checking domain on https://haveibeensquatted.com/. I wrote a simple code in Python Selenium, but it seems the page has problems with loading results in selenium - but normally (when I just click and wait) it all works. What is going on here? I do not understand why its happening. Here's the code:

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

    # === CONFIGURATION ===
    INPUT_DOMAINS = [
    "example.com",
    "nonexistent-domain-xyz123.org",
    # add more domains here
    ]
    OUTPUT_CSV = "squatted_results.csv"
    URL = "https://haveibeensquatted.com/"

    # === SET UP SELENIUM WITH CHROME ===
    options = webdriver.ChromeOptions()
    service = Service(ChromeDriverManager().install())

    driver = webdriver.Chrome(service=service, options=options)
    wait = WebDriverWait(driver, 20)
    results = []

    # XPaths
    RESULTS_READY_XPATH = "/html/body/main/div[3]/header/div/div/div[2]/div[2]/span[2]"
    ITEMS_XPATH = "/html/body/main/div[1]/div[3]/div/div/div/form/following-sibling::div//ul/li"
    NO_SQUAT_XPATH = "//div[contains(text(),'No squatted permutations')]"
    TABLE_CELL_XPATH = "/html/body/main/div[3]/div/div/div[2]/div/table/tbody/tr/td"

    try:
    for domain in INPUT_DOMAINS:
    # Load fresh page for each domain
    driver.get(URL)

    # Enter domain
    input_xpath = "/html/body/main/div[1]/div[3]/div/div/div/form/div[2]/input"
    btn_xpath = "/html/body/main/div[1]/div[3]/div/div/div/form/button"
    input_box = wait.until(EC.element_to_be_clickable((By.XPATH, input_xpath)))
    input_box.clear()
    input_box.send_keys(domain)
    driver.find_element(By.XPATH, btn_xpath).click()
    time.sleep(1000)

    finally:
    driver.quit()

    # Save results to CSV
    with open(OUTPUT_CSV, "w", newline='', encoding="utf-8") as f:
    writer = csv.writer(f)
    writer.writerow(["domain", "status"])
    writer.writerows(results)

    print(f"Done! Results saved to '{OUTPUT_CSV}'")

    Continue reading...

Compartilhe esta Página