0

I need to select the search button and Click, but I can't because it doesn't have an identifier

 <button _ngcontent-ng-cli-universal-c118 mat-mini-fab class="mat-focus-indicator ml-1 mat-ini-fab mat-button-base mat-primary ng-star-inserted">
      <span class="mat-button-wrapper">
        <mat-icon _ngcontent-ng-cli-universal-c118 role="img" class="mat-icon notranslate material-icons mat-icon-no-color" aria-hidden="true">search</mat-icon>
      </span>
      <div matripple class="mat-ripple mat-button-ripple mat-button-ripple-round"></div>
      <div class="mat-button-focus-overlay"></div>
    </button>

I use this, but it doesn't work:

driver.find_element_by_xpath(".//*[contains(text(), 'search')]").click()

Error:

Message: element not interactable
  (Session info: chrome=84.0.4147.105)
  File "Chrome1.py", line 39, in <module>
    driver.find_element_by_xpath(".//*[contains(text(), 'search')]").click()

use

button1 = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH,"//button/span/mat-icon[contains(text(),'search')]"))).click()

Error:

Message: element click intercepted: Element <mat-icon _ngcontent-ng-cli-universal-c118="" role="img" class="mat-icon notranslate material-icons mat-icon-no-color" aria-hidden="true">...</mat-icon> is not clickable at point (817, 112). Other element would receive the click: <header _ngcontent-ng-cli-universal-c35="" class="fixed-top">...</header>
  (Session info: chrome=84.0.4147.105)
  File "Chrome1.py", line 37, in <module>
    button1 = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH,"//button/span/mat-icon[contains(text(),'search')]"))).click()

1 Answers1

1

The following XPath expression will select the expected button element.

//button[span[@class="mat-button-wrapper"]/mat-icon[.="search"]]

We look for a button element with a span child containing a specific attribute and which fulfill the following condition : the content of its mat-icon child is "search".

EDIT : If it doesn't work, activate a specific expected condition, element_to_be_clickable :

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//button[span[@class="mat-button-wrapper"]/mat-icon[.="search"]]'))).click()

Imports :

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

If it still fails, use JS or AC to click on the element. With Javascript :

driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//button[span[@class="mat-button-wrapper"]/mat-icon[.="search"]]'))))

With Action Chains :

ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//button[span[@class="mat-button-wrapper"]/mat-icon[.="search"]]')))).click().perform()

Import :

from selenium.webdriver import ActionChains
E.Wiest
  • 5,425
  • 2
  • 7
  • 12