【问题标题】:Selenium click on button PythonSelenium 点击按钮 Python
【发布时间】:2020-05-15 15:56:17
【问题描述】:

我正在尝试使用 Python Selenium 单击按钮。这是以下 HTML 结构:

<div id="auth_methods">
    <fieldset data-device-index="phone1" class="">
        <h2 class="medium-or-larger auth-method-header">
            Choose an authentication method
        </h2>
        <div class="row-label push-label">
            <input type="hidden" name="factor" value="Duo Push">
            <span class="label factor-label">
                <i class="icon-smartphone-check"></i>
                Duo Push
                <small class="recommended">
                    Recommended
                </small>
            </span>
            <button class="positive auth-button" type="submit" tabindex="2"><!-- -->Send Me a Push</button>
        </div>
        <div class="row-label phone-label">
            <input type="hidden" name="factor" value="Phone Call">
            <span class="label factor-label">
                <i class="icon-call-ringing" alt="" role="presentation"></i>
                Call Me
            </span>
            <button class="positive auth-button" type="submit" tabindex="2"><!-- -->Call Me</button>
        </div>
        <div class="passcode-label row-label">
            <input type="hidden" name="factor" value="Passcode">
            <span class="label factor-label">
                <i class="icon-smartphone-ellipsis" alt="" role="presentation"></i>
                Passcode
            </span>
            <div class="passcode-input-wrapper">
                <input type="text" name="passcode" autocomplete="off" data-index="phone1" class="hidden passcode-input"
                       placeholder="ex. 867539" aria-label="passcode" tabindex="2">
                <div class="next-passcode-msg" role="alert" aria-live="polite"></div>
            </div>
            <button class="positive auth-button" type="submit" id="passcode" tabindex="2"><!-- -->Enter a Passcode
            </button>
            <input name="phone-smsable" type="hidden" value="True">
            <input name="mobile-otpable" type="hidden" value="True">
            <input name="next-passcode" type="hidden" value="None">
        </div>
    </fieldset>
    <input type="hidden" name="has-token" value="false">
</div>
<button class="positive auth-button" type="submit" tabindex="2"><!-- -->Send Me a Push</button>

如何单击最底部的“向我发送推送”?我尝试使用 xpath 或类名。 例如,我做了:

driver.find_element_by_xpath('//*[@id="auth_methods"]/fieldset/div[1]/button').click()

我也做过:

element = driver.find_element_by_class_name('positive.auth-button')

但它告诉我无法找到元素。

【问题讨论】:

    标签: python selenium button computer-science


    【解决方案1】:
    element = driver.find_element_by_css_selector("button[class='positive auth-button']")
    

    或者你可以为你的按钮添加一个 id。并使用

    element = driver.find_element_by_id('your-id-here')
    

    【讨论】:

    • selenium.common.exceptions.NoSuchElementException: 消息:没有这样的元素:无法找到元素:{"method":"css selector","selector":"button[class='positive auth-按钮']”}。它仍然给我一个错误。添加id是什么意思?如何添加 id?
    • 由于您的 html 结构,selenium 可能找不到您的按钮。试试 find_element_by_id。或者您可以尝试使用点版本的“button[class='positive.auth-button']”。我认为你应该让你的按钮独一无二。在您的按钮中, 然后使用 find_element_by_id
    • 所以我认为硒找不到我的按钮是正确的。这就说得通了。但是,如何创建一个独特的按钮?我在浏览器中编辑它吗?抱歉,我真的是 CS 新手。
    • 如果 Web 应用程序不是您的,您当然不能。然后你要么必须使用 css 选择器或 element = driver.find_elements_by_xpath("-insert-xpath-here[contains(text(), 'Send Me a Push')]") 你可以试试这个.
    【解决方案2】:

    使用下面的xpath点击

    driver.find_element_by_xpath("//button[@class='positive auth-button' and contains(.,'Send Me a Push')]").click()
    

    或者使用显式等待并等待presence_of_element_located()

    WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"/button[@class='positive auth-button' and contains(.,'Send Me a Push')]"))).click()
    

    您需要导入以下库。

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

    【讨论】:

    • 所以我尝试了两种方式。它不起作用。第一个显示无法定位元素。第二个显示 TimeoutException。
    • 检查该元素是否存在于任何 iframe 中?
    • 我不这么认为。这很奇怪。这可能是安全原因吗?
    猜你喜欢
    • 2014-02-16
    • 2016-05-08
    • 2021-09-14
    • 2018-05-11
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多