I am trying to locate Vaccination date element using Selenium so that I can pass input as per my requirement but unable to do so using By.ID or By.XPATH.
id = 'mat-input-2'
XPATH = '//\*\[@id="mat-input-2"\]'
Code:
vaccination_date = driver.find_element(By.XPATH, '//*[@id="mat-input-2"]').click()
HTML:
<div _ngcontent-lhj-c245="" class="col-md-4">
<label _ngcontent-lhj-c245="" for="vaccinationDate" class="form-label">Vaccination Date<span _ngcontent-lhj-c245="" class="mandatory-field">*</span></label>
<input _ngcontent-lhj-c245="" matinput="" formcontrolname="vaccinationDate" placeholder=" DD/MM/YYYY" class="mat-input-element mat-form-field-autofill-control mat-datepicker-input form-control vaccDate ng-pristine cdk-text-field-autofill-monitored ng-valid ng-touched" id="mat-input-2" data-placeholder=" DD/MM/YYYY" aria-invalid="false" aria-required="false" aria-haspopup="dialog" max="2024-04-21T00:00:00+05:30" data-mat-calendar="mat-datepicker-2" min="2024-02-17T05:30:00+05:30">
<mat-datepicker-toggle _ngcontent-lhj-c245="" matsuffix="" class="mat-datepicker-toggle picker" data-mat-calendar="mat-datepicker-2">
<button mat-icon-button="" type="button" class="mat-focus-indicator mat-icon-button mat-button-base" aria-haspopup="dialog" aria-label="Open calendar" tabindex="0">
<span class="mat-button-wrapper">
<svg viewBox="0 0 24 24" width="24px" height="24px" fill="currentColor" focusable="false" class="mat-datepicker-toggle-default-icon ng-star-inserted">
<path d="M19 3h-1V1h-2v2H8V1H6v2H5c-1.11 0-1.99.9-1.99 2L3 19c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm0 16H5V8h14v11zM7 10h5v5H7z"></path>
</svg>
<!---->
</span>
<span matripple="" class="mat-ripple mat-button-ripple mat-button-ripple-round"></span>
<span class="mat-button-focus-overlay"></span>
</button>
</mat-datepicker-toggle>
<mat-datepicker _ngcontent-lhj-c245=""></mat-datepicker>
<!---->
<span _ngcontent-lhj-c245="" class="alert-message ng-star-inserted"> <!----><!----><!----><!----> </span>
<!---->
</div>
I have tried this:
vaccination_date = driver.find_element(By.ID, 'mat-input-2').click()
vaccination_date = driver.find_element(By.XPATH, '//*[@id="mat-input-2"]').click()
>Solution :
It seems like you’re trying to locate the vaccination date input element using both ID and XPath, but encountering difficulties. Let’s try a couple of troubleshooting steps:
- Wait for Element to be Clickable: Sometimes, the element might not be immediately clickable due to page loading or other factors. Try waiting for the element to be clickable before clicking on it.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
vaccination_date = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'mat-input-2')))
vaccination_date.click()
-
Check if Element is within an Iframe: If the element is inside an iframe, you need to switch to that iframe before interacting with the element.
-
Check for Dynamic IDs or Classes: Sometimes, IDs or classes might be dynamically generated. In such cases, try to locate the element using other attributes that are more stable.
vaccination_date = driver.find_element(By.XPATH, '//input[@formcontrolname="vaccinationDate"]')
vaccination_date.click()
Try these steps and let me know if you encounter any issues!