Selenium单击按钮Python

2024-04-26 22:41:41 发布

您现在位置: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或类名。 例如,我做到了:

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

我还做了:

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

但它告诉我无法定位元素


Tags: namedivauthinputvaluetypebuttonlabel
2条回答
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

相关问题 更多 >