使用 Python 中的 nodriver 库与 iframe 内的按钮进行交互

问题描述 投票:0回答:1

我正在使用 Python 开发一个自动化测试项目,并使用一个名为 nodriver 的库。目前,我在选择和单击 iframe 中的按钮(特别是 reCAPTCHA 验证码)时遇到困难。

这是我尝试实现的功能的片段:

async def click_captcha(tab):
    # Trouver l'iframe du reCAPTCHA
    iframe = await tab.select('iframe[title="reCAPTCHA"]') #It works
    print("IFRAME............................;", iframe)
    tab.switch_to.frame(iframe)  # Here is the error
    button_in_iframe = await tab.select('span[id="recaptcha-anchor"]')
    await button_in_iframe.click()
    return tab

async def main():
    driver = await uc.start()
    tab = await driver.get("https://mysuper-website.com")
    tab = await click_captcha(tab)

我收到一条错误,指示 Tab 对象没有 switch_to 属性。如何正确地将上下文切换到 iframe 并使用 nodriver 与其中的按钮进行交互?是否有使用此库处理 iframe 的特定方法? 图书馆:https://github.com/ultrafunkamsterdam/nodriver

我尝试在库的代码中搜索,但没有找到任何有趣的东西

python iframe automation recaptcha
1个回答
0
投票

使用nodriver,你不需要切换到iframe来与其交互。如果在父文档中找不到搜索的文本/元素或者您正在使用多个元素选择器,则 nodriver 的搜索方法会自动切换到 iframe。

来自 nodriver 的自述文件:

通过选择器或文本进行智能元素查找,包括 iframe 内容。这也可以用作元素出现的等待条件,因为它将在找到之前重试。使用 tab.find() 按文本进行单元素查找,接受 best_match 标志,该标志不会天真地返回第一个匹配项,但会通过最接近的匹配文本长度来匹配候选者。

尽管目前,这并没有按预期进行。根据我的调试, nodriver 确实尝试在 iframe 中查找元素,但 iframe 不会返回其中的任何元素,因此不可能与其中的任何元素进行交互。回购协议所有者(Leon aka. ultrafunkamsterdam)尚未完成回购协议,因此这个问题应该在未来的版本中得到修复。你能做的就是等待。

© www.soinside.com 2019 - 2024. All rights reserved.