我需要选择“搜索”按钮并单击,但我做不到,因为它没有标识符
<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>
我用这个,但没用:
driver.find_element_by_xpath(".//*[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()
使用
button1 = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH,"//button/span/mat-icon[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()
发布于 2020-08-04 09:15:37
下面的XPath表达式将选择预期的button
元素。
//button[span[@class="mat-button-wrapper"]/mat-icon[.="search"]]
我们寻找一个button
元素,它的span
子元素包含一个特定的属性,并且满足以下条件:它的mat-icon
子元素的内容是"search
“。
编辑:如果它不能工作,激活一个特定的预期条件,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()
进口:
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"]]'))))
使用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()
进口:
from selenium.webdriver import ActionChains
https://stackoverflow.com/questions/63238692
复制相似问题