猿问

在python中使用硒选择复选框

我想在 python 中使用 selenium 选择一个复选框。以下是复选框的 HTML。将鼠标悬停在复选框上时,跨度元素会突出显示


HTML


<div id="rc-anchor-container" class="rc-anchor rc-anchor-normal rc-anchor-light">

    <div id="recaptcha-accessible-status" class="rc-anchor-aria-status" aria-hidden="true">Recaptcha requires verification. </div>

    <div class="rc-anchor-error-msg-container" style="display:none"><span class="rc-anchor-error-msg" aria-hidden="true"></span></div>

    <div class="rc-anchor-content">

        <div class="rc-inline-block">

            <div class="rc-anchor-center-container">

                <div class="rc-anchor-center-item rc-anchor-checkbox-holder"><span class="recaptcha-checkbox goog-inline-block recaptcha-checkbox-unchecked rc-anchor-checkbox" role="checkbox" aria-checked="false" id="recaptcha-anchor" tabindex="0" dir="ltr" aria-labelledby="recaptcha-anchor-label"><div class="recaptcha-checkbox-border" role="presentation"></div><div class="recaptcha-checkbox-borderAnimation" role="presentation"></div><div class="recaptcha-checkbox-spinner" role="presentation"></div><div class="recaptcha-checkbox-spinnerAnimation" role="presentation"></div><div class="recaptcha-checkbox-checkmark" role="presentation"></div></span></div>

            </div>

        </div>

        <div class="rc-inline-block">

            <div class="rc-anchor-center-container">

                <label class="rc-anchor-center-item rc-anchor-checkbox-label" aria-hidden="true" role="presentation" id="recaptcha-anchor-label"><span aria-live="polite" aria-labelledby="recaptcha-accessible-status"></span>I'm not a robot</label>

            </div>

        </div>

    </div>

    <div class="rc-anchor-normal-footer">

        <div class="rc-anchor-logo-portrait" aria-hidden="true" role="presentation">

            <div class="rc-anchor-logo-img rc-anchor-logo-img-portrait"></div>

            <div class="rc-anchor-logo-text">reCAPTCHA</div>

        </div>

我正在尝试以下代码,但它给出了以下异常selenium.common.exceptions.NoSuchElementException


温温酱
浏览 115回答 1
1回答

繁花如伊

这是一个 recaptha 的东西 .. 它不像页面中的普通元素您必须使用 selenium 导航到验证码框架..然后您可以处理复选框元素..为此,您需要先保存主窗口句柄,以便在完成 recaptcha 后返回它# save the main window handlemainwindow = browser.current_window_handle# get the recapthca iframe then navigate to itframe = browser.find_element_by_tag_name("iframe")&nbsp;&nbsp;browser.switch_to.frame(frame)# now you can access the checkbox element&nbsp;browser.find_element_by_id("recaptcha-anchor").click()# navigate back to main windowbrowser.switch_to.window(mainwindow)有关如何处理 recaptcha 挑战的更多信息,请查看此链接
随时随地看视频慕课网APP

相关分类

Python
我要回答