Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Python Selenium send_keys()更改文本格式(缩进)_Python 3.x_String_Selenium_Text_Pyperclip - Fatal编程技术网

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

Python 3.x Python Selenium send_keys()更改文本格式(缩进),python-3.x,string,selenium,text,pyperclip,Python 3.x,String,Selenium,Text,Pyperclip,我正在使用Selenium将数据输入到一些文本字段中。我遇到了send_keys()的奇怪行为,更改了我正在发送的字符串的一些格式 我通过Pyperclip使用复制粘贴解决了它,它工作正常,不会更改字符串中的任何缩进,也不会因为缩进不正确而导致错误 仍然希望了解什么是解决这个问题的正确方法,而不是我的剪贴板解决方法 driver.find_element(By.CSS_SELECTOR, ".CodeMirror textarea").send_keys(Keys.CONTROL + "a");

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

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

导入json
def检查功能(e、ans):
"""
“response”是一个包含两个键的字典,“answer”和“state”。
“answer”的值是“getGrade”返回的JSON字符串。
“state”的值是“getState”返回的JSON字符串。
单击“提交”或“保存”可注册当前状态。
"""
response=json.load(ans)
#您可以使用答案键的值来评分:
answer=json.load(响应[“应答”])
state=json.load(响应[“state”])
返回答案=0;
通过send_keys()设置的格式不正确:


导入json
def检查功能(e、ans):
"""
“response”是一个包含两个键的字典,“answer”和“state”。
“answer”的值是“getGrade”返回的JSON字符串。
“state”的值是“getState”返回的JSON字符串。
单击“提交”或“保存”可注册当前状态。
"""
response=json.load(ans)
#您可以使用答案键的值来评分:
answer=json.load(响应[“应答”])
state=json.load(响应[“state”])
返回答案=0;

您可以使用“Keys.RETURN”点击回车键……如果
Pyperclip
不是测试规范的一部分,您不应该使用
Pyperclip
作为解决方案。你能把基于Pyperclip的解决方案与这个问题联系起来吗?@DebanjanB你写的第一句话我不懂。如果我正确理解了第二句话,我的Pyperclip解决方案就在我发布的代码中。只需使用它将字符串复制到剪贴板,然后发送CTRL+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>