Search code examples
pythonseleniumselenium-chromedrivergeckodriver

Element is not located selenium python


I have selenium script that basic goal to put location in input tag and click on search button and open result page but I have problem in finding input and search button the script is below

# -*- coding: utf-8 -*-

import csv
from lxml import html
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def search_location():
    for typ in TYPESOFR:
        for loc in LOCATIONS:
            MAINBROWSER.get(typ)
            elm = WebDriverWait(MAINBROWSER, 10).until(EC.presence_of_element_located((By.ID, 'localisation')))         
            location = MAINBROWSER.find_element_by_id("localisation")
            location.click()
            location.send_keys(loc)
            search = MAINBROWSER.find_element_by_xpath('.//button[@class="sendsearch btn-blue"]')
            MAINBROWSER.execute_script("arguments[0].click();", search)

def main():
    search_location()


if __name__ == '__main__':
    # Links of types of real estates
    TYPESOFR = [
        'https://www.immoweb.be/nl/immo/huis/te-huur',
        'https://www.immoweb.be/nl/immo/appartement/te-huur',
        'https://www.immoweb.be/nl/immo/handelspand/te-huur',
        'https://www.immoweb.be/nl/immo/kantoor/te-huur',
        'https://www.immoweb.be/nl/immo/industrie/te-huur',
        'https://www.immoweb.be/nl/immo/garage/te-huur',
        'https://www.immoweb.be/nl/immo/ander/te-huur',]

    LOCATIONS = ['9000', '2000', '1000']


    chromeOptions = webdriver.ChromeOptions()
    # Disable image loading on page it will load page faster
    prefs = {"profile.managed_default_content_settings.images":2}
    chromeOptions.add_experimental_option("prefs",prefs)
    MAINBROWSER = webdriver.Chrome(chrome_options=chromeOptions)

    # MAINBROWSER = webdriver.Firefox()

    # BROWSER = webdriver.Chrome()
    main()

code is basically do is get one by one URL from TYPESOFR list and opens the link and get one by one locations from LOCATIONS list and put it in the input tag and then click on search button and follow the steps until loop completes

I have tried in both Chrome and Firefox gives both same error that element is not located


Solution

  • There is iframe tag in html so need to use switch_to function of web driver

    in above case it is used like this

    # Switching to searh iFrame
    MAINBROWSER.switch_to.frame(MAINBROWSER.find_element_by_xpath('.//*[@id="IWEB_IFRAME_ID_SEARCH"]'))
    
    # no input tag can be located by existing line
    location = MAINBROWSER.find_element_by_id("localisation")
    # ....... other logics ........