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 >= 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 >= 0;
</script>