简体   繁体   中英

How to click on an element in the iframe?

In site https://www.google.com/recaptcha/api2/demo I'm trying to click on the iframe (recaptcha) and then on the button with the headphone icon. But I can not. How to do it? Where is the mistake?

driver.get(RECAPTCHA_PAGE_URL)
time.sleep(random.uniform(MIN_RAND, MAX_RAND))
iframes = driver.find_elements_by_tag_name("iframe")
driver.switch_to_frame(iframes[0])
time.sleep(random.uniform(MIN_RAND, MAX_RAND))
driver.find_element_by_xpath('//div[@class="recaptcha-checkbox-checkmark" and @role="presentation"]').click()
time.sleep(random.uniform(MIN_RAND, MAX_RAND))
driver.find_element_by_xpath('//button[@id="recaptcha-audio-button"]').click()
time.sleep(random.uniform(LONG_MIN_RAND, LONG_MAX_RAND)) 

MIN_RAND ,MAX_RAND,LONG_MIN_RAND, LONG_MAX_RAND is nums. Error:

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: The element reference of <iframe name="etxg0ejyszkb" src="https://www.google.com/recaptcha/api2/bframe?hl=en&v=r20171129143447&k=6Le-wvkSAAAAAPBMRTvw0Q4Muexq9bi0DJwx_mJ-#etxg0ejyszkb"> stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed

You need to switch to the iframe first. Something like this

    driver.switch_to_frame(driver.find_element_by_tag_name("iframe"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM