python - 如何使用 selenium Python 复制不在 html 代码中的输入文本
问题描述
我的输入有问题,因为它的值不存储在 html 代码中。
这是html代码
<input spellcheck="false" class="editablesection" onkeypress="checkKey(event)" oninput="writinginput(this, 0)" maxlength="11" style="width: 113px;" readonly="">
我用这个搜索元素:
input = driver.find_element_by_css_selector(".editablesection")
但我无法复制文本,因为它不存储在 html 代码中。
我该怎么做?
输入其所在的站点: https ://www.w3schools.com/html/exercise.asp?filename=exercise_html_tables6
您需要单击“显示答案”按钮才能看到我要复制的文本。
解决方案
您可以使用 seleniumget_attribute("value")
方法来提取值。下面是来自 selenium python 的代码:
driver = webdriver.Chrome(PATH)
driver.get('https://www.w3schools.com/html/exercise.asp?filename=exercise_html_tables6')
driver.maximize_window()
sleep(3)
driver.find_element(By.XPATH,"//button[contains(.,'Show Answer')]").click()
sleep(3)
readOnlyField = driver.find_element(By.CSS_SELECTOR,"#showcorrectanswercontainer > .editablesection")
print(readOnlyField.get_attribute("value"))
driver.quit()
输出:
rowspan="2"
推荐阅读
- python - psycopg2.errors.SyntaxError:在“ON”处或附近出现语法错误
- neo4j - 删除具有相同属性值的重复关系,保存与 Cypher 的其他关系
- java - 在 Wiremock 中评估 XPath 表达式
- sql-server-agent - 维护作业冻结在执行状态
- r - R:编码循环结构
- wordpress - Wordpress 自定义帖子类型和帖子类型子过滤器
- javascript - 从 React 中的 API 获取数据并使用 Chart.Js 将其可视化
- java - 无法使用 Java/JSP 将数据插入 Apache Derby 数据库
- flutter - 将背景颤动的屏幕录制为流
- git - 如何将文件从一个存储库移动到另一个 git 存储库