Search code examples
pythonseleniumweb-scrapingiframeselenium-chromedriver

I am trying Selenium Webdriver to find input element by name and enter a value


I am trying to enter a value in the last box. (Numéro de châssis)

I have tried:

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

URL_BASE = "https://www.mobilit.fgov.be/WebdivPub_FR/wmvpstv1_fr?SUBSESSIONID=16382865"
browser = webdriver.Chrome(executable_path=ChromeDriverManager().install())
browser.get(URL_BASE)  
input_1 = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='Writable3']")))
input_1.send_keys("ABCD")

I got this error

  File ~\anaconda3\lib\site-packages\selenium\webdriver\support\wait.py:90 in until
    raise TimeoutException(message, screen, stacktrace)

TimeoutException

HTML:

<input type="TEXT" autocomplete="off" class="DFGUISLE Writable3" name="Writable3" id="Writable3" maxlength="17" style="dir:ltr;   position:absolute; left:416; top:446; width:235; height:22;  ">

Solution

  • Element you trying to access is inside an iframe. You first need to switch to that iframe in order to access elements inside it.
    This worked for me:

    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    options = Options()
    options.add_argument("start-maximized")
    
    
    webdriver_service = Service('C:\webdrivers\chromedriver.exe')
    driver = webdriver.Chrome(service=webdriver_service, options=options)
    url = 'https://www.mobilit.fgov.be/WebdivPub_FR/wmvpstv1_fr?SUBSESSIONID=16382865'
    driver.get(url)
    wait = WebDriverWait(driver, 20)
    
    wait.until(EC.frame_to_be_available_and_switch_to_it(driver.find_element(By.CSS_SELECTOR, "[name='AppWindow']")))
    
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='Writable3']"))).click()
    

    When you finished working inside the iframe don't forget to get back to the default content with

    driver.switch_to.default_content()