我正在尝试使用 Selenium 查找元素,但没有找到。请遵循 HTML 代码:
<div aria-disabled="false"
data-tb-test-id="DownloadCrosstab-Button"
role="button"
tabindex="0"
style="font-size: 12px; font-weight: normal; color: rgba(0, 0, 0, 0.7); display: inline-block; padding: 0px 24px; position: relative; text-align: center; border-style: solid; border-width: 1px; border-radius: 1px; height: 24px; line-height: 22px; min-width: 90px; box-sizing: border-box; outline: none; white-space: nowrap; user-select: none; cursor: default; background-color: rgba(0, 0, 0, 0); border-color: rgb(203, 203, 203); margin-top: 8px; width: 100%; -webkit-tap-highlight-color: transparent;"
>Tabela de referência cruzada</div>
我尝试过以下代码:
x = browser.find_element_by_id("Downloadcrosstab")
x = browser.find_element_by_link_text("Downloadcrosstab-Button")
x = browser.find_element_by_class_name('Crosstab')
但我得到了同样的错误:
NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":".Crosstab"}
(Session info: chrome=75.0.3770.142)
该元素似乎是一个动态元素并识别您需要诱导的元素WebDriver等待为了想要的元素可点击您可以使用以下任一解决方案:
-
Using CSS_SELECTOR
:
x = WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[data-tb-test-id='DownloadCrosstab-Button'][role='button']"))).click()
-
Using XPATH
:
x = WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@data-tb-test-id='DownloadCrosstab-Button' and text()='Tabela de referência cruzada']")))
-
Note:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Note: 您可以在以下位置找到相关讨论使用 Chrome 时的 Selenium“selenium.common.exceptions.NoSuchElementException”
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)