Python Selenium send_keys()更改文本格式(缩进)

时间:2020-01-23 20:21:05

标签: python-3.x string selenium text pyperclip

我正在使用Selenium将数据输入某些文本字段中。我遇到 send_keys() 的奇怪行为,更改了我发送的字符串的某些格式

我通过Pyperclip使用复制粘贴来解决,它工作正常,而没有更改字符串中的任何缩进,并且由于缩进不正确而导致错误。< / p>

仍然希望了解在没有剪贴板解决方法的情况下解决此问题的正确方法。

driver.find_element(By.CSS_SELECTOR, ".CodeMirror textarea").send_keys(Keys.CONTROL + "a");
driver.find_element(By.CSS_SELECTOR, ".CodeMirror textarea").send_keys(Keys.DELETE);

# driver.find_element(By.CSS_SELECTOR, ".CodeMirror textarea").send_keys(htmlFromTxt[x - 1])
# formatting bug fix
pp.copy(htmlFromTxt[x - 1])
driver.find_element(By.CSS_SELECTOR, ".CodeMirror textarea").send_keys(Keys.CONTROL + "v");

正确的原始字符串:

<problem>
  <customresponse cfn="check_function">
    <script type="loncapa/python">

import json
def check_function(e, ans):
  """
  "response" is a dictionary that contains two keys, "answer" and "state".
  The value of "answer" is the JSON string that "getGrade" returns.
  The value of "state" is the JSON string that "getState" returns.
  Clicking either "Submit" or "Save" registers the current state.
  """
  response = json.loads(ans)

  # You can use the value of the answer key to grade:
  answer = json.loads(response["answer"])
  state = json.loads(response["state"])
  return answer &gt;= 0;

</script>

通过send_keys()格式严重错误:

<problem>
    <customresponse cfn="check_function">
      <script type="loncapa/python">

        import json
        def check_function(e, ans):
          """
          "response" is a dictionary that contains two keys, "answer" and "state".
          The value of "answer" is the JSON string that "getGrade" returns.
          The value of "state" is the JSON string that "getState" returns.
          Clicking either "Submit" or "Save" registers the current state.
          """
          response = json.loads(ans)

          # You can use the value of the answer key to grade:
          answer = json.loads(response["answer"])
          state = json.loads(response["state"])
          return answer &gt;= 0;     

      </script>

0 个答案:

没有答案