0

Here is my Python code

from selenium import webdriver
import time

driver = webdriver.Chrome('/usr/local/bin/chromedriver')
driver.get("https://www.immoweb.be")
elem1 = driver.find_element_by_link_text('FRANCAIS')
elem1.click()
elem2 = driver.find_element_by_link_text('Maisons')
elem2.click()
time.sleep(2)
#searchBar = driver.find_element_by_xpath('//*[@id="localisation"]')
searchBar = driver.find_element_by_name('localisation')
searchbar.send_keys('hello')

I would like to add text in the "localisation" field, but I got the following error message.

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="localisation"]"}

I tried the exact same code on other website, and it is working properly.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Henri
  • 1,571
  • 5
  • 23
  • 38

2 Answers2

1

Your "localisation" input lives under an iframe, that's why you're not able to locate it

enter image description here

You will need to call WebDriver.switch_to() function in order to change the context to the aforementioned iframe prior to attempting interacting elements inside it.

driver.switch_to.frame("IWEB_IFRAME_ID_SEARCH")

Also consider refactoring your code to remove sleep and use Waits instead, it will make your test much faster, robust and reliable. Moreover, some elements are being loaded using AJAX technology therefore they might not be immediately available even if Selenium "thinks" that the page loading is complete.

Suggested code amendments:

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

driver = webdriver.Chrome("/usr/local/bin/chromedriver")
driver.maximize_window()
driver.get("https://www.immoweb.be")
elem1 = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.LINK_TEXT, "FRANCAIS")))
elem1.click()
elem2 = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.LINK_TEXT, "Maisons")))
elem2.click()


driver.switch_to.frame("IWEB_IFRAME_ID_SEARCH")
searchBar = elem2 = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.NAME, "localisation")))
searchBar.send_keys('hello')
driver.quit()
Dmitri T
  • 159,985
  • 5
  • 83
  • 133
0

To send a character sequence to the localisation field as the the desired element is within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.
  • Induce WebDriverWait for the desired element to be clickable.
  • You can use the following solution:

    • Code Block:

      from selenium import webdriver
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      
      chrome_options = webdriver.ChromeOptions() 
      chrome_options.add_argument("start-maximized")
      # chrome_options.add_argument('disable-infobars')
      driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
      driver.get("https://www.immoweb.be")
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "FRANCAIS"))).click()
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Maisons"))).click()
      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#IWEB_IFRAME_ID_SEARCH")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#localisation"))).send_keys('Henri')
      
  • Browser Snapshot:

Henri

Here you can find a relevant discussion on Ways to deal with #document under iframe

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352