如何到达硒中另一个元素内部的元素

How to reach element inside of another element in selenium

我正在使用 selenium webdriver 我正试图在这个源代码中达到“Veuillez valider le test reCAPTCHA..”:

<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">Veuillez valider le test reCAPTCHA..</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 class="recaptcha-checkbox-spinner-overlay"></div></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>Je ne suis pas un 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/fr/policies/privacy/" target="_blank">Confidentialité</a><span aria-hidden="true" role="presentation"> - </span><a href="https://www.google.com/intl/fr/policies/terms/" target="_blank">Conditions</a></div></div></div>
这是一个简单的网站 https://www.google.com/recaptcha/api2/demo 我想在验证码字段中找到上面的文字 我尝试通过 id 到达它,但它没有打印文本 我试图到达更大的元素,然后到达小的元素,但没有成功

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

PATH ="C:\Program Files (x86)/chromedriver.exe"
driver = webdriver.Chrome(PATH)

driver.get("https://www.google.com/recaptcha/api2/demo")
time.sleep(10)
try:
   nom = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, '//*[@id="recaptcha-demo-form"]/fieldset/ul/li[1]/label'))   
    )
   print(nom.text)
   h = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "recaptcha-accessible-status"))   
    )
   
   print(h.text)
   
except:
    driver.close()

请帮忙

您提供的第一个片段只有文本,DOM 很小,只有 ID 会带您到元素。

captcha = driver.find_element(By.ID, "recaptcha-accessible-status")

DOM Snapshot

我点击了您在 link 中显示的网站,它显示给我的是英文。这里的 DOM 比第一个片段大。

driver.find_element(By.XPATH, "//form[@id='recaptcha-demo-form']//legend"] 这应该会带您到正文。

DOM Snapshot2

如果您尝试访问验证码复选框,则必须先切换到 iframe,然后再访问该元素,因为它位于 iframe 中。

frame = driver.find_element(By.XPATH, "//iframe[@title='reCAPTCHA'])
driver.switch_to.frame(frame)
#your code here
# to come back to parent window,use:
driver.swtich_to.default_content()

iframe DOM Snapshot