Python 如何在selenium中选择按钮

Python 如何在selenium中选择按钮,python,selenium,xpath,Python,Selenium,Xpath,我需要选择搜索按钮并单击,但我不能,因为它没有标识符 <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">

我需要选择搜索按钮并单击,但我不能,因为它没有标识符

 <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>
错误:

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()
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()
错误:

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()
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()
消息:元素点击截获:元素。。。在点(817112)处不可单击。其他元素将收到单击:。。。
(会话信息:chrome=84.0.4147.105)
文件“Chrome1.py”,第37行,在
button1=WebDriverWait(driver,5)。直到(EC.element可点击((By.XPATH,“//button/span/mat icon[contains(text(),'search')]))))。点击()

以下XPath表达式将选择所需的
按钮
元素

//button[span[@class="mat-button-wrapper"]/mat-icon[.="search"]]
我们寻找一个
按钮
元素,该元素有一个
span
子元素,该子元素包含一个特定属性,并且满足以下条件:其
mat图标
子元素的内容是“
搜索

编辑:如果它不起作用,激活特定的预期条件,
元素可点击

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

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
如果仍然失败,请使用JS或AC单击该元素。使用
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"]]'))))
使用
动作链

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()
进口:

from selenium.webdriver import ActionChains

也许这篇文章可以帮助您解决不正确的xpath:但是它不起作用,那么它会引发什么错误呢?我实现了您的xpath,但是错误消息:element click intercepted:。。。在点处不可单击。。。其他元素将收到单击:…已使用更多选项编辑帖子。确保检查按钮不在iframe内。感谢Action Chains was solution@E.Wiest