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()
2
  • Maybe this post will help you resolve your incorrect xpath: stackoverflow.com/a/3655588/1123614 Commented Aug 4, 2020 at 1:06
  • but it doesn't work then what error it throws? Commented Aug 4, 2020 at 8:15

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

3 Comments

i implement your xpath, but error - Message: element click intercepted: ... </mat-icon> is not clickable at point ... Other element would receive the click: <header _ngcontent-ng-cli-universal-c35="" class="fixed-top">...</header>
Post have been edited with more options. Be sure to check the button is not inside an iframe.
Thanks with Action Chains was solution @E.Wiest

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.