0

Just starting to learn selenium using python and running into trouble with the basics of finding elements. This is a search/text box that I am trying to click into and send text to it.

Here is the code of the element I got from Edge.

<div class="box" id="ClinicSrch" style="display:block">
  <span align="lb">Last:</span> 
<input type="search" name="NameLast" size="15" class="white1" value="" maxlength="25">
  &nbsp;First: <input type="search" name="NameFirst" size="15" class="white1" value="" maxlength="25"><br>

Here is my code:

driver.find_element(By.XPATH, '//*[@id="ClinicSrch"]/input[1]').click()

The xpath that I get when copying the element from edge inspect tool is:

//*[@id="ClinicSrch"]/input[1]

Full xpath is:

/html/body/form/div[1]/div[1]/input[1]

I've been trying with other fields and buttons where I have similar xpaths but nothing is working. I always get no such element.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
WeDoneLost
  • 21
  • 2

1 Answers1

0

To locate the Last Name search field you can use either of the following locator strategies:

  • Using css_selector:

    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#PtSrchFrame[name='PtSrchFrame']")))
    element = driver.find_element(By.CSS_SELECTOR, "input[name='NameLast']")
    
  • Using xpath:

    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='PtSrchFrame' and @name='PtSrchFrame']")))  
    element = driver.find_element(By.XPATH, "//input[@name='NameLast']")
    

Ideally, to locate the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#PtSrchFrame[name='PtSrchFrame']")))
    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='NameLast']")))
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='PtSrchFrame' and @name='PtSrchFrame']")))      
    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='NameLast']")))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I added the imports you suggested. However, I still get the same no such element message when using CSS _Selector or the Xpath you suggested. I feel like I have tried it 500 different ways. – WeDoneLost Aug 26 '22 at 20:50
  • Did you try with the code in the second part? – undetected Selenium Aug 26 '22 at 20:54
  • Yes, and it throws a timeout exception error when I use the ones with webdriverwait. These are the most recent ones I have tried. element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='NameLast']"))) #element = driver.find_element(By.CSS_SELECTOR, "input[name='NameLast']") #driver.find_element(By.XPATH, '//*[@id="ClinicSrch"]/input[1]').click() #ptsearch.send_keys("able") #lastname = driver.find_element(By.NAME, "NameLast").send_keys("able") #driver.find_element(By.ID, "NameFirst").send_keys("larry") – WeDoneLost Aug 26 '22 at 21:06
  • Any other suggestions on things I can try? I still cannot make headway. Any other info you need that I could be leaving out? – WeDoneLost Aug 29 '22 at 21:07
  • See there are n number of other things among which one factor is blocking your test. If you can figure out what exactly blocking you I can bring out a solution. Watch the DOM carefully incase you can dig out the desired element is within an [**iframe**](https://stackoverflow.com/a/53276478/7429447) or a [**#shadow-root (open)**](https://stackoverflow.com/a/65055114/7429447) – undetected Selenium Aug 29 '22 at 21:13
  • Now that you say that, it most likely is in an iframe. How could I go about finding out if that is the case or what to do. Not sure where to start. Thank you! – WeDoneLost Aug 31 '22 at 14:10
  • Alright, that whole page appears to be contained within an iframe. Here is the iframe it is in, will this help? – WeDoneLost Sep 01 '22 at 18:41
  • @WeDoneLost Any thing preventing you from visiting the embedded links within my comments? Where are you exactly stuck? – undetected Selenium Sep 01 '22 at 18:46
  • Sorry that is incorrect, this is the iframe the specific element I need to find is in. – WeDoneLost Sep 01 '22 at 18:48
  • I am just trying to get started learning this and can't find a single element on the site I need to use this for. I guess I need to research how to find elements inside an iframe? – WeDoneLost Sep 01 '22 at 18:50
  • Checkout the answer update and let me know the status. – undetected Selenium Sep 01 '22 at 18:52
  • I appreciate it. I tried both and but getting the same timeout exception as before. File "C:\Users\jcoleman\PycharmProjects\pythonproject1\main.py", line 13, in WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='PtSrchFrame' and @name='PtSrchFrame']"))) File "C:\Users\jcoleman\PycharmProjects\pythonproject1\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 90, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:` – WeDoneLost Sep 01 '22 at 19:11
  • Also looking closer inspecting this page, the element may be in an iframe within 2 other iframes, so not sure if that makes a difference or not. – WeDoneLost Sep 01 '22 at 19:17
  • @WeDoneLost Ofcoarse that makes a difference. Let's discuss the issue in details within the Selenium Chat Room. – undetected Selenium Sep 01 '22 at 19:19
  • Ugh, it doesn't let me because I'm new and don't have 20 rep points. – WeDoneLost Sep 01 '22 at 19:32
  • Thank you man, I was able to figure it out after research iframes a little better. Still trying to find good ways of switching between iframes but going submit a new question. Thanks again for the help. – WeDoneLost Sep 02 '22 at 23:52