Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the readonly attribute from the input field in python with selenium

I am facing issues removing readonly tag from an input field using python and selenium. Can anybody help me here?

Datepicker Image:

Datepicker Image

HTML:

<input autocomplete="off" spellcheck="false" type="text" placeholder="Select Date and Time" readonly="readonly" class="ivu-input">

This is the code I am tried to use to remove the tag but the tag is still active during the script is running

a=driver.find_element_by_xpath('//[@id="app"]/div[3]/div/div[3]/div/div/div[2]/div/div/div/div[1]/div[2]/div/div/div/div/input')
driver.execute_script('arguments[0].removeAttribute(\"readonly\")', a);
driver.execute_script('arguments[0].setAttribute("value", "'+dateString+'")', a);
like image 900
hkoster Avatar asked Feb 26 '26 23:02

hkoster


1 Answers

To remove the readonly="readonly" attribute you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    driver.execute_script("arguments[0].removeAttribute('readonly')", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.ivu-input[placeholder='Select Date and Time']"))))
    
  • Using XPATH:

    driver.execute_script("arguments[0].removeAttribute('readonly')", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='ivu-input' and @placeholder='Select Date and Time']"))))
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

References

You can find a couple of relevant detailed discussions in:

  • How to change the date of a hidden element of a Datepicker using setAttribute method and Selenium with Python?
  • How to Change a html line code with python
  • Selenium Datepicker using JavascriptExecutor
  • Is there a way to add a new attribute with a value to an element using selenium python?
like image 110
undetected Selenium Avatar answered Mar 01 '26 13:03

undetected Selenium



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!