Python Can';在框架上找不到元素
我正在尝试创建一个登录到我帐户的机器人 在插入密码和用户名后,我使用以下代码让机器人点击“我不是机器人recaptcha”,它可以工作:Python Can';在框架上找不到元素,python,selenium,recaptcha,frame,nosuchelementexception,Python,Selenium,Recaptcha,Frame,Nosuchelementexception,我正在尝试创建一个登录到我帐户的机器人 在插入密码和用户名后,我使用以下代码让机器人点击“我不是机器人recaptcha”,它可以工作: def _delay(): time.sleep(randint(1,3)) frames = driver.find_elements_by_tag_name('iframe') driver.switch_to_frame(frames[0]) recaptcha = driver.find_element_by_xpath('//
def _delay():
time.sleep(randint(1,3))
frames = driver.find_elements_by_tag_name('iframe')
driver.switch_to_frame(frames[0])
recaptcha = driver.find_element_by_xpath('//*[@id="recaptcha-anchor"]/div[1]')
driver.execute_script("arguments[0].click();",recaptcha)
_delay()
在此之后,它将打开图像recaptcha
现在,我想尝试使用音频到文本的方法,但我无法单击图像下的“音频”按钮。以下是我使用的代码:
#finding the frame
driver.switch_to_default_content()
element_image = driver.find_element_by_xpath('/html/body')
element = element_image.find_elements_by_tag_name('iframe')
driver.switch_to_frame(element[0])
_delay()
#clicking on the "audio" button
button = driver.find_element_by_id('recaptcha-audio-button') #error
driver.execute_script("arguments[0].click();",button)
以下是输出:`Exception has accurrented:NoTouchElementException
我不知道如何点击“音频”按钮。这对我来说似乎是正确的,但仍然不起作用。
有什么建议吗?
`我怀疑发生这种情况是因为您的
delay()
函数在页面元素可见之前完成。您可以尝试增加它暂停验证的时间长度,但更好的方法是重构它以使用SeleniumWebDriverWait
对象
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.common.exceptions import ElementNotVisibleException, ElementNotSelectableException
driver = webdriver.Firefox() # or chrome...
fluent_wait = WebDriverWait(driver, timeout=10, poll_frequency=1,
ignored_exceptions=[ElementNotVisibleException,
ElementNotSelectableException])
elem = fluent_wait.until(expected_conditions.element_to_be_clickable((By.ID, "recaptcha-audio-button")))
if elem:
driver.execute_script("arguments[0].click();",button)
else:
print("couldn't find button")
什么是延迟(?是否尝试插入等待以允许加载元素?如果是这样,那就不是使用硒的正确方法。是的,很抱歉我忘了指定它_delay()是我创建的一个函数,它会等待1到3秒。谢谢你的回复:)嘿,谢谢!我试过你的解决方案,但不起作用。它给了我TimeoutException,并且没有回复“找不到按钮”。我可能用错了你的解决方案,