【问题标题】:Cannot find element in page using selenium in python3在 python3 中使用 selenium 在页面中找不到元素
【发布时间】:2023-03-24 00:13:01
【问题描述】:

我正在尝试查找元素 from this,它是一个按钮,但我无法找到该元素。我尝试通过class_namexpathid 进行查找,但似乎没有一个可以显示 selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:

<button class="dates_head commo_btn">
<!-- react-text: 175 -->
09
<!-- /react-text -->
<!-- react-text: 176 -->
<!-- /react-text -->
<!-- react-text: 177 -->
MON
<!-- /react-text -->
</button>

xpath 值应该是唯一的,但仍然无法找到元素

我在 Fedora 29 上使用 Python 3.7.2、selenium 3.141.0、chromedriver 2.40 任何有关如何解决此问题的帮助将不胜感激。谢谢你。

【问题讨论】:

    标签: python python-3.x selenium webdriver selenium-chromedriver


    【解决方案1】:

    要点击反应元素,您需要诱导 WebDriverWaitelement_to_be_clickable()

    使用以下 xpath 定位器策略。

    WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//button[@class='dates_head commo_btn'][contains(.,'09')][contains(.,'MON')]"))).click()
    

    您需要导入以下内容。

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

    请注意,如果您仍然收到timeout error,请检查是否有可用的iframe

    【讨论】:

    • 但是问题在于找到元素本身,所以只有在首先找到元素并且页面加载之后才可以点击,只有我试图找到元素
    • @Sandipan :您提供的信息非常有限,无法找出为什么没有此类元素错误即将到来。但是我已经提到了我的答案,请检查是否还有 iframe?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-08
    • 2016-07-19
    • 1970-01-01
    相关资源
    最近更新 更多