-2

Python Selenium: How to input some text in search form if its iFrame? I tried some methods, but it did not work.

search_input = driver.find_element_by_css_selector('.search-text')
ActionChains(driver).move_to_element(search_input).send_keys('indicator').perform()

and

search_input = driver.find_element_by_css_selector('.search-text')
search_input = driver.send_keys('indicator') 

switch to frame

frame = waitLoading('/html/body/div[2]/div[2]/iframe');
driver.switch_to.frame(frame)

and I use

def waitLoading(LOADING_ELEMENT_XPATH):
    WebDriverWait(driver, 5000).until(EC.presence_of_element_located((By.CSS_SELECTOR, '.blockOverlay')))
    WebDriverWait(driver, 5000).until(EC.staleness_of(driver.find_element_by_css_selector('.blockOverlay')))
    return driver.find_element_by_xpath(LOADING_ELEMENT_XPATH)

following error is

      File "test_search.py", line 53, in <module>
    search_input = driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/form/div[1]/input')
  File "/Users/annuitcoeptis/Documents/work/env/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/Users/annuitcoeptis/Documents/work/env/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
    'value': value})['value']
  File "/Users/annuitcoeptis/Documents/work/env/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/Users/annuitcoeptis/Documents/work/env/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[1]/div[2]/div/form/div[1]/input"}
RaRaRa
  • 7
  • 2
  • 1
    Did you switch to that iframe? – Andersson Oct 02 '18 at 12:11
  • yes. I switch to frame – RaRaRa Oct 02 '18 at 12:21
  • Show us how did you switch to iframe ? Your HTML code or link to website would be useful. – cruisepandey Oct 02 '18 at 12:51
  • Ok. Can you share the HTML code also ? – cruisepandey Oct 02 '18 at 12:55
  • Ok. I'll edit 1st post – RaRaRa Oct 02 '18 at 12:59
  • 1. Do you know that `WebDriverWait(driver, 5000)` means *wait for about 1,5 hours*? 2. If you want to wait until element no more present in DOM, use `WebDriverWait(driver, 5000).until_not(EC.presence_of_element_located((By.CSS_SELECTOR, '.blockOverlay')))` instead of `WebDriverWait(driver, 5000).until(EC.staleness_of(driver.find_element_by_css_selector('.blockOverlay')))` – Andersson Oct 02 '18 at 13:07
  • *"...but it did not work..."*. Can you share little more details/ – Andersson Oct 02 '18 at 13:09
  • I understood. But it immediately works, when xpath located. But I edited my code with your method. Its more laconically :) – RaRaRa Oct 02 '18 at 13:10
  • whitch details? I will share. – RaRaRa Oct 02 '18 at 13:12
  • Try to wait `WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.CSS_SELECTOR, '.search-text'))).send_keys('indicator')` or check whether there is another iframe (it's OK situation if one iframe contain another iframe - you should switch to all embeded frames one by one) – Andersson Oct 02 '18 at 13:54
  • use @ with the person name , so that they will get notified. – cruisepandey Oct 02 '18 at 14:08
  • Post the relevant HTML of the IFRAME and desired elements inside. My guess is that you are switching into the wrong IFRAME, not waiting for the elements to appear, or have a bad locator for the element. – JeffC Oct 02 '18 at 17:28

1 Answers1

1
**First you need to switch your driver on iframe**
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))


## Insert text via xpath ##
elem = driver.find_element_by_xpath("/html/body/input")
elem.send_keys("Text Enter")