首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >selenium python角中的选择按钮

selenium python角中的选择按钮
EN

Stack Overflow用户
提问于 2020-08-04 00:35:01
回答 1查看 1K关注 0票数 0

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

代码语言:javascript
运行
复制
 <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>

我用这个,但没用:

代码语言:javascript
运行
复制
driver.find_element_by_xpath(".//*[contains(text(), 'search')]").click()

错误:

代码语言:javascript
运行
复制
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()

使用

代码语言:javascript
运行
复制
button1 = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH,"//button/span/mat-icon[contains(text(),'search')]"))).click()

错误:

代码语言:javascript
运行
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-04 09:15:37

下面的XPath表达式将选择预期的button元素。

代码语言:javascript
运行
复制
//button[span[@class="mat-button-wrapper"]/mat-icon[.="search"]]

我们寻找一个button元素,它的span子元素包含一个特定的属性,并且满足以下条件:它的mat-icon子元素的内容是"search“。

编辑:如果它不能工作,激活一个特定的预期条件,element_to_be_clickable

代码语言:javascript
运行
复制
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//button[span[@class="mat-button-wrapper"]/mat-icon[.="search"]]'))).click()

进口:

代码语言:javascript
运行
复制
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

代码语言: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

代码语言:javascript
运行
复制
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()

进口:

代码语言:javascript
运行
复制
from selenium.webdriver import ActionChains
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63238692

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档