Selenium Issue – Unable to locate element: {“method”:”css selector”,”selector” – Need help fixing this

I need to fix this code. This comes with an error message saying

 NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="login_username"]"}
  (Session info: chrome=92.0.4515.159)

The below is the code

   from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    # finq credentials
    username = "xxxxxxx@gmail.com"
    password = "xxxxxxxxx"
    # initialize the Chrome driver
    driver = webdriver.Chrome(executable_path=r'C:Userssnyderdatachromedriver.exe')
    # head to finq login page
    driver.get("https://www.finq.com/en/login")
    # find username/email field and send the username itself to the input field
    driver.find_element_by_id("login_username").send_keys(username)
    # find password input field and insert password as well
    driver.find_element_by_id("login_password").send_keys(password)
    # click login button
    driver.find_element_by_name("submit_login").click()

Answer

It is in iframe, so you need to switch to it before interaction.

you can use the below css_selector to switch to iframe :

iframe[id='login']

in code :

username = "xxxxxxx@gmail.com"

driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.implicitly_wait(5)
driver.get("https://www.finq.com/en/login")
wait = WebDriverWait(driver, 10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[id='login']")))
# find username/email field and send the username itself to the input field
driver.find_element_by_id("login_username").send_keys(username)
# find password input field and insert password as well
driver.find_element_by_id("login_password").send_keys(password)
# click login button
driver.find_element_by_css_selector("button[id='submit_login']").click()

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC