Home > Enterprise >  Unable to change angular element text/value using Selenium and Python
Unable to change angular element text/value using Selenium and Python

Time:11-17

enter image description here I have this specific element which is a date input field. It doesn't have a calendar or any dropdown or selection type. As always I started with the basic driver.find_element_by_id('dataInicio').send_keys(date_value) and it doesn't work, it's weird because it doesn't event raise an error, it actually send_keys the element, but this send_keys seen to change no attribute or property in the element. Then I tried print the value attribute and alright, I can reach that, it returns the standard filled value of this control, it also can reach the attribute value by setting it, but again, it changes nothing when setting attribute value to a specific date_value it changes nothing(after that I tried return the value property by printing and no change). Also tried to search the standard filled value in the whole html page and ha: Nothing, no where.

I could actually change it's property value by this specific line:

nvg.execute_script("arguments[0].value = '"   end_month_date   "';", end_date_field)

Then I print the value property/attribute and it returns the 'end_month_date' value, BUT, whenever I click to start the download it seen not to understand the inputted 'end_month_date' value, and instead, generates a download with the standard value from the field.

CodePudding user response:

The desired element is a Angular element, so ideally to send a character sequence to the element you need to induce WebDriverWait for the element_to_be_clickable() for the dynamic element to completely render within the HTML DOM and you can use either of the following Locator Strategy:

  • Using CSS_SELECTOR:

    WebDriverWait(nvg, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#dataInicio[name='dataInicio'][ng-model='filterDtFrom'][max-date='filterDtTo']"))).send_keys(end_month_date)
    
  • Using XPATH:

    WebDriverWait(nvg, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='dataInicio' and @name='dataInicio'][@ng-model='filterDtFrom' and @max-date='filterDtTo']"))).send_keys(end_month_date)
    
  • Using execute_script():

    • Using CSS_SELECTOR:

      end_date_field = WebDriverWait(nvg, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#dataInicio[name='dataInicio'][ng-model='filterDtFrom'][max-date='filterDtTo']")))
      nvg.execute_script("arguments[0].value = '"   end_month_date   "';", end_date_field)
      
    • Using XPATH:

      end_date_field = WebDriverWait(nvg, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='dataInicio' and @name='dataInicio'][@ng-model='filterDtFrom' and @max-date='filterDtTo']")))
      nvg.execute_script("arguments[0].value = '"   end_month_date   "';", end_date_field)
      
  • 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
    
  • Related