首页 > 解决方案 > 如何使用 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

您需要单击“显示答案”按钮才能看到我要复制的文本。

标签: pythonselenium

解决方案


您可以使用 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"

推荐阅读