gpt4 book ai didi

python - 使用 Selenium 和 Python 查找存在 data-tb-test-id 属性而不是 id 的元素

转载 作者:太空宇宙 更新时间:2023-11-04 11:15:28 24 4
gpt4 key购买 nike

我正在尝试使用 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)

最佳答案

该元素似乎是一个动态元素并标识您需要诱导 WebDriverWait 以使所需的元素可点击的元素,您可以使用以下任一方法解决方案:

  • 使用 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()
  • 使用 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']")))
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC

Note: You can find a relevant discussion in Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome

关于python - 使用 Selenium 和 Python 查找存在 data-tb-test-id 属性而不是 id 的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57101417/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com