Python Selenium Checking Checkboxes



为了我的生命,我可以让chromedriver点击所有这些复选框。要么是找不到,要么是无法交互。

<form class="step__form step__block" action="creation-full/step/legal-and-opt-ins" method="post" id="flow-form" enctype="multipart/form-data" novalidate="">
<input type="hidden" name="csrftoken" id="flow-csrftoken" value="52d14a6a-69d7-4c06-bbeb-3418c331892e">
<label class="step__field--label step__form__block">
<input class="step__checkbox" value="true" id="capture-opt-in-third-party-news-special-offers" data-capture-id="opt-in-third-party-news-special-offers" name="opt-in-third-party-news-special-offers" type="checkbox">
<input type="hidden" name="opt-in-third-party-news-special-offers" value="false"> <span class="step__field__indicator--checkbox"> <svg class="svg-inline--fa fa-check fa-w-16" aria-hidden="true" focusable="false" data-prefix="far" data-icon="check" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" data-fa-i2svg=""><path fill="currentColor" d="M435.848 83.466L172.804 346.51l-96.652-96.652c-4.686-4.686-12.284-4.686-16.971 0l-28.284 28.284c-4.686 4.686-4.686 12.284 0 16.971l133.421 133.421c4.686 4.686 12.284 4.686 16.971 0l299.813-299.813c4.686-4.686 4.686-12.284 0-16.971l-28.284-28.284c-4.686-4.686-12.284-4.686-16.97 0z"></path></svg><!-- <i class="far fa-check"></i> --> </span> <span class="step__field--label__text">Account email will receive carefully selected news, event information and special offers about third-party products and services.</span> </label>
<div class="step__legal-checkboxes step__form__block" id="legal-checkboxes">
<input type="hidden" name="tou-agreements-implicit" value="08b946df-660a-40e4-a072-1fbde65173b1;209144" data-capture-id="tou-agreements-implicit" checked="">
<input type="hidden" name="tou-agreements-implicit" value="966f03a4-29e1-440c-b142-e54ee091e52d;93047" data-capture-id="tou-agreements-implicit" checked="">
<input type="hidden" name="tou-agreements-implicit" value="cd5930c0-2784-420c-a23d-1e0d6ff8599b;110052" data-capture-id="tou-agreements-implicit" checked="">
<label class="step__field--label step__form__block">
<input class="step__checkbox" value="41e60b3d-244d-4776-be75-e2c6b3eba9a3;187105" data-capture-id="tou-agreements-implicit" name="tou-agreements-implicit" type="checkbox" checked="">
<input type="hidden" name="tou-agreements-implicit" value=""> <span class="step__field__indicator--checkbox"> <svg class="svg-inline--fa fa-check fa-w-16" aria-hidden="true" focusable="false" data-prefix="far" data-icon="check" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" data-fa-i2svg=""><path fill="currentColor" d="M435.848 83.466L172.804 346.51l-96.652-96.652c-4.686-4.686-12.284-4.686-16.971 0l-28.284 28.284c-4.686 4.686-4.686 12.284 0 16.971l133.421 133.421c4.686 4.686 12.284 4.686 16.971 0l299.813-299.813c4.686-4.686 4.686-12.284 0-16.971l-28.284-28.284c-4.686-4.686-12.284-4.686-16.97 0z"></path></svg><!-- <i class="far fa-check"></i> --> </span> <span class="step__field--label__text">I have reviewed and understand the Privacy Policy. For more information about how we use information and your rights to object, see our <a href="https://www.blizzard.com/legal/41e60b3d-244d-4776-be75-e2c6b3eba9a3" rel="external noopener" target="_blank">Blizzard Entertainment® Privacy Policy <svg class="svg-inline--fa fa-external-link fa-w-16" aria-hidden="true" focusable="false" data-prefix="far" data-icon="external-link" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" data-fa-i2svg=""><path fill="currentColor" d="M497.6,0,334.4.17A14.4,14.4,0,0,0,320,14.57V47.88a14.4,14.4,0,0,0,14.69,14.4l73.63-2.72,2.06,2.06L131.52,340.49a12,12,0,0,0,0,17l23,23a12,12,0,0,0,17,0L450.38,101.62l2.06,2.06-2.72,73.63A14.4,14.4,0,0,0,464.12,192h33.31a14.4,14.4,0,0,0,14.4-14.4L512,14.4A14.4,14.4,0,0,0,497.6,0ZM432,288H416a16,16,0,0,0-16,16V458a6,6,0,0,1-6,6H54a6,6,0,0,1-6-6V118a6,6,0,0,1,6-6H208a16,16,0,0,0,16-16V80a16,16,0,0,0-16-16H48A48,48,0,0,0,0,112V464a48,48,0,0,0,48,48H400a48,48,0,0,0,48-48V304A16,16,0,0,0,432,288Z"></path></svg><!-- <i class="far fa-external-link"></i> --></a> </span> </label>
</div>
<div class="step__legal-summary step__block end">
<div class="step__legal-summary__links step__block" id="legal-summary-links"> La La La </div>
</div>
<div class="step__button-container step__block end">
<button type="submit" class="step__button step__button--primary" id="flow-form-submit-btn">Continue</button>
</div>

任何帮助将不胜感激。由于

您可以使用此id capture-opt-in-third-party-news-special-offers,这在本质上是唯一的。

如果python是你的绑定语言,那么这应该可以工作:

driver.find_element_by_css_selector("#legal-checkboxes label input").click()

或者如果需要显式等待:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 50)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#legal-checkboxes label input"))).click()

或者如果两者都不起作用,试试JS:-

check_box = driver.find_element_by_css_selector("#legal-checkboxes label input")
driver.execute_script("arguments[0].click();", check_box)

最新更新