在python中使用selenium选中复选框

2024-04-25 17:11:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我想选择一个在python中使用selenium的复选框。下面是复选框的HTML。将鼠标悬停在复选框上时,span元素将高亮显示

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>
        <div class="rc-anchor-pt"><a href="https://www.google.com/intl/en/policies/privacy/" target="_blank">Privacy</a><span aria-hidden="true" role="presentation"> - </span><a href="https://www.google.com/intl/en/policies/terms/" target="_blank">Terms</a></div>
    </div>
</div>

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

我的代码

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

chromedriver = 'C:\Program Files (x86)\Google\Chrome\chromedriver'
browser = webdriver.Chrome(chromedriver)
browser.get(url)

checkBox = browser.find_element_by_id("recaptcha-anchor")
checkBox.click()

Tags: dividtrueseleniumpresentationrecaptchalabelhidden
1条回答
网友
1楼 · 发布于 2024-04-25 17:11:39

这是重述的东西。。它不像页面中的普通元素

您必须使用selenium导航到验证码帧。。然后可以处理checkbox元素。。你知道吗

要做到这一点,首先需要保存主窗口句柄,以便在处理完recaptcha后返回到它

# save the main window handle
mainwindow = browser.current_window_handle

# get the recapthca iframe then navigate to it
frame = browser.find_element_by_tag_name("iframe")  
browser.switch_to.frame(frame)

# now you can access the checkbox element 
browser.find_element_by_id("recaptcha-anchor").click()

# navigate back to main window
browser.switch_to.window(mainwindow)

有关如何处理recaptcha挑战的更多信息,请查看此link

相关问题 更多 >