Unable to locate element using Xpath using selenium in python

Question:

I have tried several ways to locate the elements of the form on the [webpage ] using selenium in python but I unable to do so.

This is the code I have tried (only for "Name" column) along with several ways to input the xpath as suggested in other answers.

import time
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()
driver.get("https://shooliniuniversity.com/how-to-apply")
driver.maximize_window()

# fill the form
Name = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='Name']" )))
Name.send_keys('John Doe')


# time.sleep(2)
# driver.quit()

I also tried using By.NAME, "Name" and By.ID, 'input text'

Kindly help and suggest me how shall I correct this?
Also, I am believing that the suggestion can be applied on all the other fields of the form too.

Asked By: vision96

||

Answers:

Your locator strategy was all correct. Only thing you were missing was handling the iframe. As this element is wrapped within an iframe, you need to switch into it first, then perform the next actions.

Try the below code:

iframe = driver.find_element(By.XPATH, "(//iframe)[1]")
wait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(iframe))
Name = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='Name']" )))
Name.send_keys('John Doe')

Output:

enter image description here

NOTE:
Once you are done with all actions within the iframe and want to action on the other part of the page, then you need to switch back to main content using below code:

driver.switch_to.default_content()
Answered By: Shawn