Как выбрать кнопку в селене python angular - PullRequest
0 голосов
/ 04 августа 2020

Мне нужно выбрать кнопку поиска и щелкнуть, но я не могу, потому что у него нет идентификатора

 <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()

1 Ответ

1 голос
/ 04 августа 2020

Следующее выражение XPath выберет ожидаемый элемент button.

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

Мы ищем элемент button с дочерним span, содержащим атрибут c и который выполняет следующее условие: содержимое его mat-icon дочернего элемента - «search».

РЕДАКТИРОВАТЬ: Если это не сработает, активируйте заданное c ожидаемое условие, 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 или A C, чтобы щелкнуть элемент. С 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...