gpt4 book ai didi

python - 在 python 中使用 selenium 选择复选框

转载 作者:行者123 更新时间:2023-12-05 02:10:41 30 4
gpt4 key购买 nike

我想在 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()

最佳答案

这是一个recaptha的东西..它不像页面中的普通元素

你必须使用 selenium 导航到验证码框架..然后你可以处理复选框元素..

要做到这一点,您首先需要保存主窗口句柄,以便在完成 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

关于python - 在 python 中使用 selenium 选择复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58440159/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com