0

I want to get the text from an input element with text disabled.

In the URL below I'm able to write some text to input text box and disable the text but I've tried several ways to get the text in the disabled text box but I'm not sure why is not working.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

url="https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_text_disabled2"
driver_path="/cygdrive/d/d/Python/chromedriver"
driver = webdriver.Chrome(driver_path)
driver.get (url)
driver.set_window_position(0, 0)
driver.set_window_size(1552, 852)

driver.switch_to.frame("iframeResult")
driver.find_element_by_xpath("//*[@id='myText']").send_keys("test123") # write text to input box
driver.find_element_by_css_selector("body > button").click() # Click on button to disable text

I've tried these options even using javascript (that works in Chrome console) but doesn't work with Selenium Python.

driver.execute_script("document.querySelector('#myText').value")
driver.find_element_by_id("myText")).first_selected_option.text
driver.find_element_by_id("myText").getAttribute("value");

>>> driver.find_element_by_id("myText").getAttribute("value")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'WebElement' object has no attribute 'getAttribute'
>>>

Thanks in advane for any help.

3
  • 2
    Try changing getAttribute to get_attribute Commented Jul 23, 2019 at 18:05
  • You could try removing the disabled attribute via javascript. (btw, the only way to get this value would be via javascript.) Selenium only reads the markup, and it's "value" attribute would be the same after the field is filled. Commented Jul 23, 2019 at 18:39
  • @DainiusPreimantas Excellent. Thank you for the correction. You could put as answer. Commented Jul 23, 2019 at 18:45

1 Answer 1

1

Python

element.get_attribute("attribute name")

Java

element.getAttribute("attribute name")

Reference: How to get attribute of element from Selenium?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.