繁体   English   中英

Selenium 点击按钮 Python

[英]Selenium click on button Python

我正在尝试使用 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 或 class 名称。 例如,我做了:

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

我也做了:

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

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

element = driver.find_element_by_css_selector("button[class='positive auth-button']")

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

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

使用下面的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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM