使用硒在带有表单控件的框中输入文本

时间:2019-02-24 23:27:49

标签: python-3.x selenium selenium-webdriver beautifulsoup

我有下面的代码试图将文本以水平形式输入到单元格中。我正在使用硒,页面中的来源类似于下面的示例来源。当我运行下面的代码尝试通过id标识元素时,出现以下错误。有人知道这个问题可能是什么,您能建议如何解决吗?

来源:

<div class="form-group" id="questionaire0"><div class="col-sm-11 question"><h5>Please write your Full Name<small> (Briefly respond below)</small></h5></div><div class="col-sm-11"><textarea name="questionaire0" class="form-control"></textarea></div></div>

代码:

driver.find_element_by_id("questionaire0").send_keys(‘Test Name’)

time.sleep(1)

错误:

---------------------------------------------------------------------------
WebDriverException                        Traceback (most recent call last)
<ipython-input-54-c78e15aec19b> in <module>()
      1 
      2 
----> 3 driver.find_element_by_id("questionaire0").send_keys('Test Name')
      4 
      5 time.sleep(1)

~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py in send_keys(self, *value)
    477         self._execute(Command.SEND_KEYS_TO_ELEMENT,
    478                       {'text': "".join(keys_to_typing(value)),
--> 479                        'value': keys_to_typing(value)})
    480 
    481     # RenderedWebElement Items

~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py in _execute(self, command, params)
    626             params = {}
    627         params['id'] = self._id
--> 628         return self._parent.execute(command, params)
    629 
    630     def find_element(self, by=By.ID, value=None):

~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
    318         response = self.command_executor.execute(driver_command, params)
    319         if response:
--> 320             self.error_handler.check_response(response)
    321             response['value'] = self._unwrap_value(
    322                 response.get('value', None))

~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243 
    244     def _value_or_default(self, obj, key, default):

WebDriverException: Message: unknown error: cannot focus element
  (Session info: chrome=72.0.3626.109)
  (Driver info: chromedriver=2.42.591059 (a3d9684d10d61aa0c45f6723b327283be1ebaad8),platform=Mac OS X 10.12.6 x86_64)

1 个答案:

答案 0 :(得分:1)

您的Textarea字段没有名为id的属性。您使用属性name。它应该起作用。

driver.find_element_by_name("questionaire0").send_keys('Test Name')