Home > Net >  How to copy the text of an input, which is not in the html code with selenium Python
How to copy the text of an input, which is not in the html code with selenium Python

Time:11-10

I have a problem with a input, because its value do not store in html code.

This is the html code

<input spellcheck="false" class="editablesection" onkeypress="checkKey(event)" oninput="writinginput(this, 0)" maxlength="11" style="width: 113px;" readonly="">
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Image of input with text

I search the element with this:

input = driver.find_element_by_css_selector(".editablesection")

But i cant copy the text because its not store in html code.

How i can do it?

Site where input its located: https://www.w3schools.com/html/exercise.asp?filename=exercise_html_tables6

You need to click "Show Answer" button to see the text i want to copy.

CodePudding user response:

You can use selenium get_attribute("value") method to extract the value. Below is the code from 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()

Output:

rowspan="2"
  • Related