我正在尝试使用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')
但是它告诉我无法定位元素。
答案 0 :(得分:0)
element = driver.find_element_by_css_selector("button[class='positive auth-button']")
,也可以为按钮添加ID。并使用
element = driver.find_element_by_id('your-id-here')
答案 1 :(得分:0)
使用以下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