Python Selenium:如果其iFrame如何在搜索表单中输入一些文本?

时间:2018-10-02 12:04:37

标签: python selenium

Python Selenium:如果其iFrame如何以搜索形式输入一些文本? 我尝试了一些方法,但是没有用。

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

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

切换到框架

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

我用

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)

以下错误是

      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"}

1 个答案:

答案 0 :(得分: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")