0

I am trying to write 'eureka' in a field, and Selenium always triggers the same error: NoSuchElementException

This is the field where I am trying to get the data from:

<input type="text" class="standardTextField" id="rulesFilternameValue" name="rulesFilternameValue" onclick="if(typeof event !== 'undefined' &amp;&amp; event != null) {event.cancelBubble=true;}if(typeof event !== 'undefined' &amp;&amp; event != null) {event.cancelBubble=true;}" onkeypress="if (event.keyCode === 13){event.preventDefault();handleFilterRequest('rulelist', 'FILTER')}">

I have already tried with find_element_by_id, find_element_by_class, find_element_by_xpath and nothing.

The whole thing is inside an iFrame:

<iframe id="mainwindow" name="mainwindow" style="top: 54px; height: 849px;" 
src="/h2/welcomeAction.do?misc=1582634309181" frameborder="0"></iframe>

but event find_element_by_id('mainwindow') fails

Guy
  • 46,488
  • 10
  • 44
  • 88
error404
  • 63
  • 1
  • 10
  • Does this answer your question? [Select iframe using Python + Selenium](https://stackoverflow.com/questions/7534622/select-iframe-using-python-selenium) – Naveen Feb 25 '20 at 13:17
  • unfortunately not: browser.switch_to.frame(browser.find_element_by_tag_name("iframe")) -> NoSuchElementException: Message: Unable to locate element: iframe – error404 Feb 25 '20 at 13:26
  • you have to redirect your selenium driver to the proper frame. You said it is an iframe, which means that selenium isn't aware of it. So find the different frames available to the driver on the page and select the iframe. Then do your `find_element` – William Bright Feb 25 '20 at 13:38
  • Doubt: when I do switch_to.frame(browser.find_element_by_tag_name("iframe")) I also get a NoSuchElementException – error404 Feb 25 '20 at 13:51
  • I was wrong and you were right guys. Sorry and thanks a lot for the help :-) – error404 Feb 25 '20 at 15:15

1 Answers1

1

As the the desired element is within an <iframe> so to invoke click() on the element you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.
  • Induce WebDriverWait for the desired element to be clickable.
  • You can use the following solution:

    • Using CSS_SELECTOR:

      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#mainwindow[name='mainwindow']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.standardTextField#rulesFilternameValue"))).send_keys("eureka")
      
    • Using XPATH:

      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='mainwindow' and @name='mainwindow']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='standardTextField' and @id='rulesFilternameValue']"))).send_keys("eureka")
      
    • 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
      

Reference

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352