How to click on an hCaptcha checkbox inside a nested iframe using Python Selenium?

I’m trying to automate the process of clicking on an hCaptcha checkbox that is nested inside three different iframes. I have tried various approaches, but I haven’t been successful. Here are the details of the iframes and the target checkbox:

  1. The checkbox is located inside the third iframe.
  2. The XPath of the first iframe: /html/body/div[1]/iframe
  3. The XPath of the second iframe: /html/body/div[1]/div[4]/div/div/main/div[2]/div/div[2]/iframe
  4. The XPath of the third iframe: ./html/body/div[1]/iframe
  5. The ID of the target checkbox: 'checkbox'

I have tried switching to each iframe using the switch_to.frame() method and then locating the checkbox using various methods such as find_element_by_id() and find_element_by_xpath(). However, none of my attempts have been successful.

Could anyone provide guidance on how to click on the hCaptcha checkbox inside the third iframe using Python Selenium? Any help would be greatly appreciated.

first_iframe = driver.find_element(By.XPATH, "/html/body/div[1]/iframe")
driver.switch_to.frame(first_iframe)
print("1. iframe")

time.sleep(2)
second_iframe = driver.find_element(By.XPATH, "/html/body/div[1]/div[4]/div/div/main/div[2]/div/div[2]/iframe")
driver.switch_to.frame(second_iframe)
print("2. iframe")

time.sleep(2)

# Ana sayfadaki iframe'e geçiş yapın
third_iframe = driver.find_element(By.XPATH, "./html/body/div[1]/iframe")
driver.switch_to.frame(third_iframe)

print("3. iframe")
time.sleep(2)

driver.switch_to.default_content()
driver.find_element(By.XPATH, '//*[@id="checkbox"]').click()
# driver.find_element(By.ID, "checkbox").click()
time.sleep(2)


js_code = "document.getElementById('h-captcha-response-0n9nbu0cse7').style.display = 'block';"
driver.execute_script(js_code)
time.sleep(5)
captcha = driver.find_element(By.XPATH, "h-captcha-response-0n9nbu0cse7").send_keys("test")