如何使用selenium和python将文本插入到输入标记中

时间:2017-01-08 20:38:34

标签: python selenium

我能够按类名找到该元素,但我不确定如何选择它并向其发送文本。

当前代码:

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

元素我试图选择:

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

错误:

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)

2 个答案:

答案 0 :(得分:2)

您尝试选择的元素没有类名。尝试

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')

答案 1 :(得分:0)

editor.click()之前尝试editor.send_keys('text'),看看它是否有效。