[Python] Selenium Headless vrs Non Headless. I can't get Selenium to set a field while using...

Discussão em 'Python' iniciado por Stack, Agosto 2, 2025.

  1. Stack

    Stack Membro Participativo

    I saw I needed to add a window size however that did not fix my issue here is the working Non headless code

    # _-_-_- Non Headless -_-_-_
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    import webbrowser

    def GetFAAapdURL(APD):
    driver = webdriver.Chrome()
    driver.get("https://www.faa.gov/airports/runway_safety/diagrams/")
    input_field = driver.find_element(By.ID, "ident") # Replace with actual ID
    input_field.send_keys(APD)
    input_field.send_keys(Keys.RETURN)
    table = driver.find_element(By.XPATH, "//table") # Adjust XPath as needed
    links = table.find_elements(By.TAG_NAME, "a")
    for link in links:
    href = link.get_attribute("href")
    if href: # Ensure the href is not None
    if "aeronav.faa" in href:
    print(href)
    return href

    webbrowser.open(GetFAAapdURL("ATL"))


    Abd now the Headless version

    # _-_-_- Headless -_-_-_
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.chrome.options import Options
    from selenium import webdriver
    import webbrowser

    def GetFAAapdURL_Headless(APD):
    chrome_options = Options()
    chrome_options.add_argument("--headless") # Run Chrome in headless mode
    chrome_options.add_argument('--window-size=1920x1080')
    driver = webdriver.Chrome(options=chrome_options)

    driver.get("https://www.faa.gov/airports/runway_safety/diagrams/")
    input_field = driver.find_element(By.ID, "ident")
    input_field.send_keys(Keys.RETURN)
    table = driver.find_element(By.XPATH, "//table") # Adjust XPath as needed
    links = table.find_elements(By.TAG_NAME, "a")
    for link in links:
    href = link.get_attribute("href")
    if href: # Ensure the href is not None
    if "aeronav.faa" in href:
    print(href)
    return href

    webbrowser.open(GetFAAapdURL_Headless("ATL"))


    here is the line that errors

    input_field = driver.find_element(By.ID, "ident")


    I tried to minimize the window and that has a flashing of the window at start and still errors. So not a great solution.

    Continue reading...

Compartilhe esta Página