Home > Back-end >  How to reach element inside of another element in selenium
How to reach element inside of another element in selenium

Time:02-02

i'm using selenium webdriver i'm trying to reach that " Veuillez valider le test reCAPTCHA.. "in this source code:

<div id="rc-anchor-container" ><div id="recaptcha-accessible-status"  aria-hidden="true">Veuillez valider le test reCAPTCHA..</div><div  style="display:none"><span  aria-hidden="true"></span></div><div ><div ><div ><div ><span  role="checkbox" aria-checked="false" id="recaptcha-anchor" tabindex="0" dir="ltr" aria-labelledby="recaptcha-anchor-label"><div  role="presentation"></div><div  role="presentation"></div><div  role="presentation"><div ></div></div><div  role="presentation"></div></span></div></div></div><div ><div ><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 ><div  aria-hidden="true" role="presentation"><div ></div><div >reCAPTCHA</div></div><div ><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>
here is the website its a simple website https://www.google.com/recaptcha/api2/demo i wanna reach the text above in the captcha field i tried reaching it by id but it didnt print the text itried to reach the bigger element then reach the small one but it didnt wrk

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()

please help

CodePudding user response:

The first snippet you provided has just text, and the DOM is small, and just the ID would take you to the element.

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

DOM Snapshot

I hit the website you showed in the link, and it shows to me in English. Here the DOM is relatively larger than the first snippet one.

driver.find_element(By.XPATH, "//form[@id='recaptcha-demo-form']//legend"] This should take you to the text.

DOM Snapshot2

If you are trying to get to the CAPTCHA checkbox, then you have to switch to iframe first and then access the element, as it lies inside an 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

  •  Tags:  
  • Related