簡體   English   中英

如何使用selenium Python復制不在html代碼中的輸入文本

[英]How to copy the text of an input, which is not in the html code with selenium Python

我的輸入有問題,因為它的值不存儲在 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

您需要單擊“顯示答案”按鈕才能查看我要復制的文本。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM