python-3.x - Python Selenium send_keys() 更改文本格式(缩进)
问题描述
我正在使用 Selenium 将数据输入到一些文本字段中。我遇到了 的奇怪行为send_keys()
,更改了我发送的字符串的一些格式。
我通过 Pyperclip 使用 Copy-Paste 解决了这个问题,它工作正常,没有更改我的字符串中的任何缩进并且由于不正确的缩进而导致错误。
仍然希望在没有我的剪贴板解决方法的情况下了解解决此问题的正确方法。
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 >= 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 >= 0;
</script>
解决方案
推荐阅读
- javascript - 页面加载 HTML 上的活动选项卡问题
- accessibility - 为了满足WCAG,我应该改变公司颜色吗?
- docker - 监控 Kind 集群内 Kubernetes pod 上的流量
- google-bigquery - 如何避免 BigQuery 刷新令牌过期
- swift - Apple CareKit 商店为挽救患者而崩溃:无效的核心数据 URI
- node.js - 通过来自 mongodb 的节点 api 从数据调用中获取角度组件控制器中的特定项目
- flutter - 从 Main 方法推送自定义的通用未处理错误路由
- html - :hover 时放置导航栏未打开
- android - onBillingSetupFinished 从未调用过
- ionic-framework - 制作个人资料页面