因此,我试图在iframe中单击按钮,但只是错误地指出找不到该按钮。
SRV
在最后一行,我尝试获取ID为recaptcha = driver.find_element_by_tag_name('iframe')
driver.switch_to.frame(recaptcha)
submit1 = driver.find_element_by_id('recaptcha-anchor')
actions = ActionChains(driver)
actions.move_to_element(submit1)
actions.click(submit1).perform()
time.sleep(3)
driver.find_element_by_id('recaptcha-audio-button').click()
的元素。
正如我在上面所说的那样,只是错误而找不到它。
我要尝试使用的网站是:https://patrickhlauke.github.io/recaptcha/
为什么我不能单击此按钮?
谢谢,伊拉克。
编辑 这是错误
recaptcha-audio-button
答案 0 :(得分:1)
元素位于不同的iframe中,因此您可以这样做:
代码:
# go back to main window
driver.switch_to.default_content()
# find all frames and switch to second frame
all_frames = driver.find_elements_by_tag_name('iframe')
driver.switch_to.frame(all_frames[1])
driver.find_element_by_id('recaptcha-audio-button').click()
最少的工作代码,因此每个人都可以复制和测试它
import selenium.webdriver
import time
driver = selenium.webdriver.Firefox()
driver.get('https://patrickhlauke.github.io/recaptcha/')
recaptcha = driver.find_element_by_tag_name('iframe')
driver.switch_to.frame(recaptcha)
submit1 = driver.find_element_by_id('recaptcha-anchor')
actions = selenium.webdriver.ActionChains(driver)
actions.move_to_element(submit1)
actions.click(submit1).perform()
time.sleep(3)
# go back to main window
driver.switch_to.default_content()
# find all frames and switch to second frame
all_frames = driver.find_elements_by_tag_name('iframe')
driver.switch_to.frame(all_frames[1])
driver.find_element_by_id('recaptcha-audio-button').click()