'Is it possible to use python Webdriver to access an element from a mat-select dropdown?

I am trying to build a python function that will select a specific item from a dropdown list in a mat-select:

(<mat-select role="combobox" aria-autocomplete="none" aria-haspopup="true" name="serviceId" formcontrolname="serviceId" class="mat-select ng-tns-c122-1 ng-tns-c88-0 mat-select-empty ng-pristine ng-invalid ng-star-inserted mat-select-invalid ng-touched" aria-labelledby="mat-form-field-label-1 mat-select-value-1" id="mat-select-0" tabindex="0" aria-expanded="false" aria-required="false" aria-disabled="false" aria-invalid="true"><div cdk-overlay-origin="" class="mat-select-trigger ng-tns-c122-1"><div class="mat-select-value ng-tns-c122-1" id="mat-select-value-1"><span class="mat-select-placeholder mat-select-min-line ng-tns-c122-1 ng-star-inserted"></span><!----><!----></div><div class="mat-select-arrow-wrapper ng-tns-c122-1"><div class="mat-select-arrow ng-tns-c122-1"></div></div></div><!----></mat-select>)

I have tried using a normal select:

(select = Select(driver.find_element_by_xpath(xpath to mat-select)) select.select_by_visible_text(visibletext of desired element)) But I can't seem to find anything online about how to do this, therefore just wondering if it is possible...



Solution 1:[1]

mat-select and select are two different tag.

Hence the code that you've here

Select(driver.find_element_by_xpath(xpath to mat-select))

won't work since you are using Select class from Selenium that has been build to take care of select tag in the HTMLDOM.

Solution:

to able to click on mat-select you should first click on drop down using Selenium and then click on the available option through selenium.

Code:

wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//mat-select[@role='combobox' and @name='serviceId']"))).click() # clicked on main drop down thus the option could be visible
wait.until(EC.element_to_be_clickable((By.XPATH, "option xpath here"))).click()

Imports:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 cruisepandey