Search code examples
pythonseleniumselenium-webdriverxpathiframe

NoSuchElement have tried every path type


I have trying to input an email into the login section of the page and can not get it to work.

I have the same error everytime "NoSuchElement"

The website is https://accela.kerncounty.com/CitizenAccess/Default.aspx

I have tried XPath, Name and ID

def wait(y):
    for x in range(y):
        print(x)
        time.sleep(1)


driver.get(website)
print("Website Loaded")
wait(10)
driver.find_element(By.XPATH, "/html/body/form/div[3]/div/div[7]/div[1]/table/tbody/tr/td[2]/div[2]/div[2]/table/tbody/tr/td/div/div[2]/table/tbody/tr/td[1]").send_keys("[email protected]")
print("Input Email")

Solution

  • There are several issues here:

    1. Element you trying to access is inside an iframe. So, you first have to switch into it in order to access elements there.
    2. You need to use correct locators. /html/body/form/div[3]/div/div[7]/div[1]/table/tbody/tr/td[2]/div[2]/div[2]/table/tbody/tr/td/div/div[2]/table/tbody/tr/td[1] does not match nothing on that page. Locators must be short and clear, not absolute paths.
    3. You need to use WebDriverWait expected_conditions explicit waits, not a hardcoded sleeps.
      The following code works:
    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, 20)
    
    url = "https://accela.kerncounty.com/CitizenAccess/Default.aspx"
    driver.get(url)
    wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "ACAFrame")))
    
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[id*='LoginBox_txtUserId']"))).send_keys("[email protected]")
    

    This is what this code does: enter image description here

    When finished working inside the iframe don't forget to switch back to the default content with:

    driver.switch_to.default_content()