Home > Blockchain >  Access the date text of input element with Selenium
Access the date text of input element with Selenium

Time:11-15

When visiting the website this element contains the date: 20210930

<input placeholder="yyyymmdd" ng-class="{'mark-red': relatedDocument.documentDate == null}" title="yyyyMMdd" data-autoclose="1" bs-datepicker="" data-date-format="yyyyMMdd" data-date-type="number" class="form-control importInputHalf ng-valid ng-valid-date ng-valid-min ng-valid-max ng-touched ng-dirty ng-valid-parse" data-max-date="today" ng-model="relatedDocument.documentDate" ng-disabled="editDisabled || relatedDocument.documentDuplicated || (isDocumentAlreadyUsed(relatedDocument) &amp;&amp; relatedDocument.rex) || relatedDocument.correctionCloneLocked" ng-change="documentChanged(relatedDocument,'documentDate')">

But when I try to access this information through Selenium with the following code it returns empty:

date = self.driver.find_element(By.XPATH, "// *[@id='out']/table/tbody/tr[3]/td[5]/input").text

Is there a way to access this data?

CodePudding user response:

The text 20210930 isn't the innerText but the value within. So text attribute won't be able to retrive it. You need to use get_attribute("value") inducing WebDriverWait as follows:

  • Using CSS_SELECTOR:

    print(WebDriverWait(self.driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[placeholder='yyyymmdd'][title='yyyyMMdd'][ata-date-format='yyyyMMdd'][data-max-date='today']"))).get_attribute("value"))
    
  • Using XPATH:

    print(WebDriverWait(self.driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[@placeholder='yyyymmdd' and @title='yyyyMMdd'][@data-date-format='yyyyMMdd' and @data-max-date='today']"))).get_attribute("value"))
    
  • 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