简体   繁体   中英

How to insert text into an input tag using selenium and python

I'm able to find the element by class name that this resides in but I'm not sure how to select it itself and send text to it.

Current Code:

editor = browser.find_element_by_class_name('editor')
editor.send_keys('text')

Element I'm trying to select:

<input type="text" tabindex="103" placeholder="" style="width: 444px;">

Error:

File "C:\Python35\lib\site-packages\selenium\webdriver\remote\webelement.py", line 347, in send_keys
    self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': keys_to_typing(value)})
  File "C:\Python35\lib\site-packages\selenium\webdriver\remote\webelement.py", line 494, in _execute
    return self._parent.execute(command, params)
  File "C:\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "C:\Python35\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot focus element
  (Session info: chrome=55.0.2883.87)
  (Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Windows NT 10.0.14393 x86_64)

Element that you try to select doesn't have class name. Try

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

editor = WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.XPATH, '//input[@type="text"][@tabindex="103"]')))
editor.send_keys('text')

editor.send_keys('text') editor.click()之前尝试使用editor.click()并查看它是否有效。

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