Python Selenium send_keys () меняет форматирование текста (отступ) - PullRequest
0 голосов
/ 23 января 2020

Я использую Selenium для ввода данных в некоторые текстовые поля. Я столкнулся со странным поведением send_keys(), меняя некоторые из форматирования строки, которую я отправляю.

I решено это с помощью вместо этого использую Copy-Paste через Pyperclip, который работает нормально, не меняя отступ в моей строке и не вызывая ошибку из-за неправильного отступа.

Все еще надеясь понять, что является правильным способом решить это без моего обхода буфера обмена.

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>
...