Search code examples
seleniumxpathiframeautomationcss-selectors

Selenium - Having Problems finding a nz-select form element, when trying to click


I am currently trying to automate the input into a form on a website, but i cant seem to find a way to select the dropdown.

On website:

immosuche.degewo.de/de/properties/W1400-40660-0750-0902.html

You'll need to click on Kontaktieren.

In HTML:

I'm currently trying to find it by xpath this way:

driver.findElement(By.xpath("/html/body/el-root/div/el-listing-application/form/div[2]/div[1]/nz-form-item/nz-form-control/div/span/nz-select/div")).click();

But i always get this exception:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/el-root/div/el-listing-application/form/div[2]/div[1]/nz-form-item/nz-form-control/div/span/nz-select/div"}
  (Session info: chrome=108.0.5359.126)
For documentation on this error, please visit: https://selenium.dev/exceptions/#no_such_element

Does any one have any idea how i could click it without getting the Exeption?


Solution

  • Those elements are inside an iframe. To access elements inside it you need to switch into that iframe first.
    The following code sample is working:

    import time
    
    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(options=options, service=webdriver_service)
    wait = WebDriverWait(driver, 10)
    
    url = "https://immosuche.degewo.de/de/properties/W1400-40660-0750-0902.html"
    driver.get(url)
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".expose__header-functions a[href='#kontakt']"))).click()
    time.sleep(3)
    wait.until(EC.frame_to_be_available_and_switch_to_it((By.TAG_NAME, "iframe")))
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[formcontrolname='salutation']"))).click()
    wait.until(EC.element_to_be_clickable((By.XPATH, "//li[contains(.,'Herr')]"))).click()
    wait.until(EC.element_to_be_clickable((By.ID, "firstName"))).send_keys('Prophet')
    wait.until(EC.element_to_be_clickable((By.ID, "lastName"))).send_keys('Mozes')
    wait.until(EC.element_to_be_clickable((By.ID, "email"))).send_keys('[email protected]')
    wait.until(EC.element_to_be_clickable((By.ID, "formly_2_input_numberPersonsTotal_0"))).send_keys('5')
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".form-actions [type='submit']"))).click()
    

    The result screenshot is:

    enter image description here

    The request is sent.
    When finished working inside the iframe don't forget to switch to the default content with:

    driver.switch_to.default_content()