【问题标题】:python Selenium NoSuchElementException: Message: no such element: Unable to locate elementpython Selenium NoSuchElementException:消息:没有这样的元素:无法找到元素
【发布时间】:2017-09-15 08:21:23
【问题描述】:
<h2>Account Management</h2>
<table class="form-table">
<tr id="password" class="user-pass1-wrap">
    <th><label for="pass1">New Password</label></th>
    <td>
        <input class="hidden" value=" " /><!-- #24364 workaround -->
        <button type="button" id="wp-generate-pw" class="button button-
secondary wp-generate-pw hide-if-no-js">Generate Password</button>
        <div class="wp-pwd hide-if-js">
            <span class="password-input-wrapper">

在上面的源代码中,我想找到 id 为“id='wp-generate-pw”的按钮。 我试图通过 xpath 定位

classButtonXpath= "//*[@id='wp-generate-pw']"
siteClassNameElement = self.driver.find_element_by_xpath(classButtonXpath)
siteClassNameElement.click()

但我收到以下错误:NoSuchElementException: 消息:没有这样的元素:无法找到元素:{"method":"xpath","selector":"//*[@id='wp-generate-pw ']"

我尝试了很多方法,但还是找不到

有什么想法吗? 谢谢

【问题讨论】:

    标签: selenium


    【解决方案1】:

    如果目标table 元素是动态生成的,您可能需要等到您的按钮出现在DOM 中:

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait as wait
    from selenium.webdriver.support import expected_conditions as EC
    
    wait(self.driver, 10).until(EC.element_to_be_clickable((By.ID, "wp-generate-pw"))).click()
    

    如果这种方法也不起作用,您可以检查table 是否位于iframe 内。如果是这样,你应该在点击button之前切换到iframe

    self.driver.switch_to.frame("frameID") # replace "frameID" with actual value
    classButtonXpath= "//*[@id='wp-generate-pw']"
    siteClassNameElement = self.driver.find_element_by_xpath(classButtonXpath)
    siteClassNameElement.click()
    self.driver.switch_to.default_content()
    

    【讨论】:

    • 嗨,非常感谢,但第一个解决方案不起作用。关于 iframe,我检查过,表格不在 iframe 内。其他想法?
    • 你能分享页面URL吗?
    • 恐怕我做不到。它是一个带有私人凭据的私人页面
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多