簡體   English   中英

如何使用 Selenium 和 Python 單擊帶有文本的按鈕向我發送推送?

[英]How to click the button with text Send Me a Push using Selenium and Python?

</head>
<body>
<div class="">
<div class="base-wrapper ">

<div class="base-main">
<div role="main" class="base-body">

<form action="/frame/prompt" method="post" id="login-form" class="inline login-form hidden">
<input type="hidden" name="sid" value="ZjUwNzUzYjA4MjVkNDc4OGJkODUyZThjOTVhMzM3ZTA&#x3d;&#x7c;70.181.192.163&#x7c;1610702974&#x7c;3040365d5035c5288c02b1c5abd3bb6bae523af0">
<input type="hidden" name="url" value="/frame/prompt">
<input type="hidden" name="enrollment_message" value="">
<input type="hidden" name="itype" value="adfs">

<input type="hidden" name="ukey" value="DUOF1NY48TBKCWXBRTRF">

<input type="hidden" name="out_of_date" value="">
<input type="hidden" name="days_out_of_date" value="">
<input type="hidden" name="should_update_dm" value="">


<input type="hidden" name="preferred_factor" value="">
<input type="hidden" name="preferred_device" value="">

<input type="hidden" name="days_to_block" value="None">
<input type="hidden" name="should_retry_u2f_timeouts" value="False">

<input type="hidden" id="has_phone_that_requires_compliance_text" name="has_phone_that_requires_compliance_text" value="False">

<fieldset class="device-selector hidden">
<h1 class="cramped-frame-view">
Device&#x3a;
</h1>
<div class="device-select-wrapper">
<select name="device" aria-label="Device" tabindex=2>





<option value="phone1">iOS &#x28;XXX-XXX-5065&#x29;</option>


</select>

</div>
</fieldset>

<div id="auth_methods">

<fieldset data-device-index="phone1" class="hidden">
<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&#x20;Push">
<span class="label factor-label">
<i class="icon-smartphone-check"></i>
Duo Push


<small class="recommended">
Recommended
</small>


</span>
<button type="submit" class="auth-button&#x20;positive" tabindex="2" ><!-- -->Send Me a Push </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.&#x20;867539" aria-label="passcode" tabindex=2>
<div class="next-passcode-msg" role="alert" aria-live="polite"></div>
</div>
<button type="submit" class="auth-button&#x20;positive" id="passcode" tabindex="2" ><!-- -->Enter a Passcode </button>
<input name="phone-smsable" type="hidden" value="False" />
<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>

<div>

</div>
</form>


</div>
</div>
<div class="base-navigation">

<div class="base-navigation">
<div role="banner">

<a href="&#x2f;frame&#x2f;prompt&#x3f;sid&#x3d;ZjUwNzUzYjA4MjVkNDc4OGJkODUyZThjOTVhMzM3ZTA&#x3d;&#x7c;70.181.192.163&#x7c;1610702974&#x7c;3040365d5035c5288c02b1c5abd3bb6bae523af0">
<img src="&#x2f;frame&#x2f;logo&#x3f;sid&#x3d;ZjUwNzUzYjA4MjVkNDc4OGJkODUyZThjOTVhMzM3ZTA&#x3d;&#x7c;70.181.192.163&#x7c;1610702974&#x7c;3040365d5035c5288c02b1c5abd3bb6bae523af0" alt="CSUSM&#x20;Authentication" width="128"/>
</a>

</div>
<div class="help-sidebar">
<button class='btn btn-support'>
<i class="icon-align-justify" aria-label="Open"></i>
<i class="icon-delete" aria-label="Close"></i>
Settings
</button>
<div class="help-links">
<nav role="navigation">

<a id="help_link" class="help-nav" href="https&#x3a;&#x2f;&#x2f;guide.duo.com&#x2f;prompt" target="_blank" aria-label="What&#x20;is&#x20;the&#x20;Duo&#x20;Prompt&#x3f;&#x20;&#x28;Opens&#x20;in&#x20;a&#x20;new&#x20;tab.&#x29;">
What is this&#x3f;<i class="icon-new-window"></i>
</a>


<a class="help-nav" id="new-device" href="/frame/enroll/pre_flow_prompt?sid=ZjUwNzUzYjA4MjVkNDc4OGJkODUyZThjOTVhMzM3ZTA%3D%7C70.181.192.163%7C1610702974%7C3040365d5035c5288c02b1c5abd3bb6bae523af0&post_auth_action=addDevice">
Add a new device
</a>
<a class="help-nav" href="/frame/enroll/pre_flow_prompt?sid=ZjUwNzUzYjA4MjVkNDc4OGJkODUyZThjOTVhMzM3ZTA%3D%7C70.181.192.163%7C1610702974%7C3040365d5035c5288c02b1c5abd3bb6bae523af0&post_auth_action=manageDevices">
My Settings &amp; Devices
</a>

<a href="#" class="need-help">Need help&#x3f;</a>
</nav>

<div role="contentinfo">
<a href="https&#x3a;&#x2f;&#x2f;duo.com" class="branding-link" target="_blank">
Powered by Duo Security
</a>
</div>


</div>

</div>
<div class="help-overlay offscreen">
<div class="help-links">
<nav role="navigation">

<a id="help_link" class="help-nav" href="https&#x3a;&#x2f;&#x2f;guide.duo.com&#x2f;prompt" target="_blank" aria-label="What&#x20;is&#x20;the&#x20;Duo&#x20;Prompt&#x3f;&#x20;&#x28;Opens&#x20;in&#x20;a&#x20;new&#x20;tab.&#x29;">
What is this&#x3f;<i class="icon-new-window"></i>
</a>


<a class="help-nav" id="new-device" href="/frame/enroll/pre_flow_prompt?sid=ZjUwNzUzYjA4MjVkNDc4OGJkODUyZThjOTVhMzM3ZTA%3D%7C70.181.192.163%7C1610702974%7C3040365d5035c5288c02b1c5abd3bb6bae523af0&post_auth_action=addDevice">
Add a new device
</a>
<a class="help-nav" href="/frame/enroll/pre_flow_prompt?sid=ZjUwNzUzYjA4MjVkNDc4OGJkODUyZThjOTVhMzM3ZTA%3D%7C70.181.192.163%7C1610702974%7C3040365d5035c5288c02b1c5abd3bb6bae523af0&post_auth_action=manageDevices">
My Settings &amp; Devices
</a>

<a href="#" class="need-help">Need help&#x3f;</a>
</nav>

<div role="contentinfo">
<a href="https&#x3a;&#x2f;&#x2f;duo.com" class="branding-link" target="_blank">
Powered by Duo Security
</a>
</div>


</div>

</div>
</div>

</div>
<div class="base-sidebar">

</div>
<div id="messages-view" class="hidden">
<div class="messages-list">

</div>
</div>


</div>
</div>
<script id="translations" type="text/json">
{"domain": "js-messages", "locale_data": {"js-messages": {"": {"domain": "js-messages", "plural_forms": "nplurals&#x3d;2&#x3b; plural&#x3d;&#x28;n &#x21;&#x3d; 1&#x29;&#x3b;", "lang": "en"}}}}
</script>


<script src="&#x2f;frame&#x2f;static&#x2f;shared&#x2f;lib&#x2f;jquery&#x2f;jquery-prologue.js&#x3f;v&#x3d;400dc"></script>
<script src="&#x2f;frame&#x2f;static&#x2f;shared&#x2f;lib&#x2f;jquery&#x2f;jquery-legacy.min.js&#x3f;v&#x3d;72e7b"></script>
<script src="&#x2f;frame&#x2f;static&#x2f;shared&#x2f;lib&#x2f;he&#x2f;he.min.js&#x3f;v&#x3d;aaa33"></script>
<script src="&#x2f;frame&#x2f;static&#x2f;js&#x2f;lib&#x2f;jquery-postmessage.min.js&#x3f;v&#x3d;98c73"></script>
<script src="&#x2f;frame&#x2f;static&#x2f;shared&#x2f;lib&#x2f;lodash&#x2f;lodash.min.js&#x3f;v&#x3d;14516"></script>
<script src="&#x2f;frame&#x2f;static&#x2f;shared&#x2f;lib&#x2f;backbone&#x2f;backbone-min.js&#x3f;v&#x3d;28a93"></script>
<script src="&#x2f;frame&#x2f;static&#x2f;js&#x2f;page&#x2f;v3&#x2f;frame.js&#x3f;v&#x3d;82022"></script>
<script src="&#x2f;frame&#x2f;static&#x2f;js&#x2f;page&#x2f;v3&#x2f;base.js&#x3f;v&#x3d;d6293"></script>
<script src="&#x2f;frame&#x2f;static&#x2f;shared&#x2f;lib&#x2f;validator&#x2f;validator.min.js&#x3f;v&#x3d;9a068"></script>
<script src="&#x2f;frame&#x2f;static&#x2f;shared&#x2f;lib&#x2f;jquery&#x2f;jquery-epilogue.js&#x3f;v&#x3d;c4ac5"></script>
<script id="browser_exceptions" src="&#x2f;frame&#x2f;static&#x2f;shared&#x2f;js&#x2f;errors.js&#x3f;v&#x3d;96827" data-url="/frame/browser_exceptions"></script>
<input type="hidden" name="helpdesk-message" id="helpdesk-message" value="Please&#x20;visit&#x20;https&#x3a;&#x2f;&#x2f;www.csusm.edu&#x2f;iits&#x2f;iitsforyou&#x2f;multifactor-authentication&#x2f;index.html&#x20;for&#x20;videos&#x20;and&#x20;guides&#x20;for&#x20;faculty,&#x20;staff,&#x20;and&#x20;students." />
<!--[if lt IE 10]>
<script src="&#x2f;frame&#x2f;static&#x2f;js&#x2f;page&#x2f;v3&#x2f;quirks.js&#x3f;v&#x3d;1ed89"></script>
<!--<![endif]-->


<script src="&#x2f;frame&#x2f;static&#x2f;js&#x2f;lib&#x2f;jquery.tipsy.js&#x3f;v&#x3d;c0432"></script>
<script src="&#x2f;frame&#x2f;static&#x2f;js&#x2f;page&#x2f;v3&#x2f;prompt.js&#x3f;v&#x3d;3f751"></script>


<!--[if lt IE 9]>
<script src="&#x2f;frame&#x2f;static&#x2f;js&#x2f;lib&#x2f;html5shiv.js&#x3f;v&#x3d;86fbf"></script>
<![endif]-->
</body>
</html>

我正在嘗試單擊該行上的按鈕

<button type="submit" class="auth-button&#x20;positive" tabindex="2" ><!-- -->Send Me a Push </button>

到目前為止我已經嘗試過:

iframe = driver.find_element_by_xpath("//iframe[@id='duo_iframe']")

driver.switch_to.frame(iframe)

那么以下

driver.find_element_by_css_selector(".row-label").click()

或者

driver.find_element_by_xpath("//div[@class='row-labeldiv.row-label:nth-child(2)']/button[.='Send Me a Push']").click()

或者

driver.find_element_by_xpath("//button[text()='Send Me a Push']").click()

無論我嘗試什么,selenium 都找不到元素。 我不確定我錯過了什么。 我知道我嘗試單擊的按鈕沒有唯一的 class。 我在 stackoverflow 上找到的最明顯的解決方案是使用按鈕內的文本來單擊它,但是即使切換到正確的 iframe(?),我仍然找不到該元素。

我假設我在檢查 chrome 上的元素並找到后正確切換到 iframe

<iframe id="duo_iframe" src="https://api-b22f1fa2.duosecurity.com/frame/web/v1/auth?tx=TX|Y3N1c21cY2Fub24wMTB8RElBWlU2VzJCOUFRTExEQ1pCWDZ8MTYxMDcwMzI0MA==|883b56e2b24048e526a973c70d5bdb359db905d1&amp;parent=https%3A%2F%2Fauth.csusm.edu%2Fadfs%2Fls%2F%3FSAMLRequest%3DnZJBTwIxEIX%252Fyqb3pbtdINAACUqMJKhE0IMXM3QHabJt105r9N%252B77GpEDxw8NZ2Z7%252BXNy0wITFXLeQwHe4%252BvESkk76ayJNvGlEVvpQPSJC0YJBmU3MxvVlL0Mll7F5xyFTtBzhNAhD5oZ1myXEzZ804MxkIVGYwFCiXyYTYY9%252FvjkdrtixyGxWgIOOoXwzxjySN6asgpa4QanCji0lIAG5pSJvI0y9N8sM0KKYTM8ieWLJpttIXQUocQapKcQ7NqT1Ek08Mycij3xCviLJl%252Fe7t0lqJBv0H%252FphU%252B3K9%252BaKVOWNKmrvC4NTeujBX26kPN2z91r0hBUVstcQ%252BxCinVLFl%252F5Xahbanty%252FnIdt0Qyevtdp2u7zZbNpsctWUbgZ%252F9w5rBACUE%252BONswk91J91p3DaOlou1q7T6SK6cNxDOGz5WdJnu21EZPFjSaAPjs07%252F97XNPgE%253D%26RelayState%3Dhttps%253A%252F%252Fcc.csusm.edu%252Fauth%252Fsaml%252Findex.php%26SigAlg%3Dhttp%253A%252F%252Fwww.w3.org%252F2001%252F04%252Fxmldsig-more%2523rsa-sha256%26Signature%3DWAdtwVP3PFqkG5AOq0gspZJPT06j6VOrKiK1p3RqsMWqug7xj4NZaHLf4ZAkhLoFdw0kIsV7EAWxi1KHRGi0AvUGoyue5EeEo6TqEuzHDBLOITdLR0xIvCjbmlXT0KfAsX1f2Vrm0tn4FPH7VETu%252BpjKQDM15TwPkfdGAWs5Eqr2ZzA9GDI5Lwg9GNakWpBqNvhC0gQ5gDiPKxigGvyLw1V5xFSOQqn4Xx5uUDvSUezAFPxB6yq72a5e0NzTmc%252FVRRliiqAjH4xMRagTgdOOruXIwBPBFlUmzy8ySChKAGjAOuXgq8uz0vEjDuYcSd%252F7A0IrBrN5GLVVrqTiqKxoSA%253D%253D&amp;v=2.6" frameborder="0"></iframe>

封裝我試圖按下的按鈕,selenium 也找到並切換它。

利用:

driver.find_element_by_xpath("//button[contains(text(),'Send Me a Push')"]).click()

there is no iframe in the html you added in the question, if the html added was the content inside iframe then switch to iframe and then use the mentioned locator

要單擊帶有文本作為向我發送推送的元素,您可以使用以下任一定位器策略

  • 使用css_selector

     driver.find_element(By.CSS_SELECTOR, "button[class^='auth-button'][type='submit']").click()
  • 使用xpath

     driver.find_element(By.XPATH, "//button[starts-with(@class, 'auth-button') and contains(., 'Send Me a Push')]").click()

理想情況下,要單擊需要為element_to_be_clickable()誘導WebDriverWait的元素,您可以使用以下任一Locator Strategies

  • 使用CSS_SELECTOR

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[class^='auth-button'][type='submit']"))).click()
  • 使用XPATH

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[starts-with(@class, 'auth-button') and contains(., 'Send Me a Push')]"))).click()
  • 注意:您必須添加以下導入:

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

我嘗試了下面的代碼並且它有效:

driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@id='duo_iframe']")));
Thread.sleep(3000);
driver.findElement(By.xpath("//div[@id='auth_methods']/fieldset/div[1]/button")).click();

我認為 iframe 的 url 中可能有一個# ,如下所示: www.someurl.com/page/#/something ,嘗試刪除#www.someurl.com/page/something

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM