【问题标题】:How to click on an element in the iframe?如何单击 iframe 中的元素?
【发布时间】:2017-12-07 15:44:08
【问题描述】:

在站点https://www.google.com/recaptcha/api2/demo 中,我尝试单击 iframe(recaptcha),然后单击带有耳机图标的按钮。但是我不能。怎么做?哪里错了?

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 是数字。 错误:

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

【问题讨论】:

    标签: python selenium iframe


    【解决方案1】:

    您需要先切换到 iframe。像这样的

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

    【讨论】:

    • 这个 iframe 有#document。到那里怎么走? @SuperStew
    • 你应该也可以通过 Xpath 找到它。
    猜你喜欢
    • 2019-06-01
    • 2021-05-12
    • 1970-01-01
    • 2020-11-04
    • 2020-05-24
    • 1970-01-01
    • 1970-01-01
    • 2015-10-05
    • 1970-01-01
    相关资源
    最近更新 更多