简体   繁体   中英

Unable to enter text in the text box in a python selenium script

I am trying to enter a comment in the following text box with details as

<input type="text" name="com_data" value="" size="40">

I tried the following:

driver.find_element_by_class_name("Comment:").send_keys("hello")
# driver.find_element_by_name('btnSubmit').click()
# driver.find_element_by_name("com_data").send_keys("hello smirajka")
driver.find_element_by_xpath("//input[@name='com_data']").send_keys("hello")

All of the above attempts failed with error message as:

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@name='com_data']"}
  (Session info: chrome=55.0.2883.95)
  (Driver info: chromedriver=2.25.426935 (820a95b0b81d33e42712f9198c215f703412e1a1),platform=Mac OS X 10.11.6 x86_64)

To handle element inside an iframe , try to switch to this iframe before sending text:

driver.switch_to_frame('iframe_id_or_name')
driver.find_element_by_xpath("//input[@name='com_data']").send_keys("hello")

If there is no id / name set to iframe :

driver.switch_to_frame(driver_find_element_by_tag_name('iframe'))

If there are multiple iframe elements on page, you can access them by index:

driver.switch_to_frame(driver_find_elements_by_tag_name('iframe')[0])

To switch back you might need to use:

driver.switch_to_default_content()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM