Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Message: invalid selector: An invalid or illegal selector was specified error using Selenium Python

I’m trying to click on this element:

<td role="gridcell" class="mat-calendar-body-cell mat-calendar-body-active ng-star-inserted" tabindex="0" data-mat-row="0" data-mat-col="1" aria-label="May 2, 2022" aria-selected="true" style="width: 14.2857%; padding-top: 7.14286%; padding-bottom: 7.14286%;"><div class="mat-calendar-body-cell-content mat-focus-indicator mat-calendar-body-selected mat-calendar-body-today"> 2 </div><div class="mat-calendar-body-cell-preview"></div></td>

To do so, I’d like to select it via the element’s aria-label (May 2, 2022). So I have a variable that contains the string "May 2, 2022" (the string is called theElement), and I plug it into a find_element function.

Why is this code snippet giving me the:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Message: invalid selector: An invalid or illegal selector was specified

error?

Code trials:

driver.find_element(by=By.CSS_SELECTOR, value="[aria-label="+theElement+"]")

>Solution :

To locate the <td> element through the value of the aria-label attribute i.e. May 2, 2022 you can use either of the following Locator Strategies:

  • Using css_selector:

    element = driver.find_element(By.CSS_SELECTOR, "td[aria-label='May 2, 2022']")
    
  • Using xpath:

    element = driver.find_element(By.XPATH, "//td[@aria-label='May 2, 2022']")
    
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading