0

I'm using Selenium to enter data into some textfields. I encountered strange behavior of send_keys(), changing some of the formatting of the string I'm sending.

I solved it by using Copy-Paste via Pyperclip instead, which works fine, without changing any indentation in my string and causing an error due to incorrect indentation.

Still hoping to understand what is the right way to solve this without my clipboard workaround.

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");

Original and correct string:

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

Badly formatted via 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>
Eric T
  • 1
  • 1
  • you can use "Keys.RETURN" to hit the enter key... – pcalkins Jan 23 '20 at 22:51
  • if `Pyperclip` wasn't a part of the _Test Specs_ you shouldn't have settled with using `Pyperclip` as a solution. Can you link the `Pyperclip` based solution within this question? – undetected Selenium Jan 24 '20 at 08:10
  • @DebanjanB I didn't understand the first sentence you wrote. And if I understood the second sentence correctly, my Pyperclip solution is within the code I posted. Just using it to copy the string to the clipboard and then sending CTRL + V to paste into the text field. – Eric T Jan 24 '20 at 12:36

0 Answers0