Selenium can't find a form element by name

Viewed 322

What am I doing wrong here? The browser opens and goes to the page, but I get the following error message. I have tried xpath; I have tried by id...

Traceback (most recent call last):

File "", line 1, in ms()

File "C:/Users/mohsen/Google Drive/scraper/Code/ICRC.py", line 47, in ms ICRC_GetLinks()

File "C:/Users/mohsen/Google Drive/scraper/Code/ICRC.py", line 72, in ICRC_GetLinks EC.presence_of_element_located((By.NAME, "search_last_name")))

File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace)

TimeoutException

The code is:

link = "https://iccrc-crcic.ca/find-a-professional/"

    # this will open up new window with the url provided above 
driver = webdriver.Chrome()

driver.get(link)

sleep(1)

WebDriverWait(driver,30).until(
     EC.presence_of_element_located((By.NAME, "search_last_name")))


driver.find_element_by_name("search_last_name").send_keys("Smith")
1 Answers

The element is inside an iframe you need to switch. Induce WebDriverWait() and wait for for frame_to_be_available_and_switch_to_it() and follwoing css selector.

driver.get("https://iccrc-crcic.ca/find-a-professional/")
wait=WebDriverWait(driver,20)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src='https://secure.iccrc-crcic.ca/search-new/EN']"))) 
wait.until(EC.element_to_be_clickable((By.XPATH,"//input[@name='search_last_name']"))).send_keys("user1")

To jump out from iframe you need to use

driver.switch_to.default_content()

Browser snapshot: enter image description here

Related