【问题标题】:playwright (Python) click in a seemingly generated frame?剧作家(Python)点击一个看似生成的框架?
【发布时间】:2021-12-30 17:03:55
【问题描述】:

我正在尝试使用 playwright 与设备中的页面进行交互。从设备显示的初始页面中,我从我看到的 html 中选择我想要的选项卡,使用 inspect with

page.click('id=laConfig')

对于显示的代码sn -p

<div id="nav" class="nav" style="width: 974px;">
        <span class="menu" id="iMenu3" onclick="ChangeFrame('log.asp',3)">
            <label id="laLog" name="laLog" class="mousepointer">Log</label>
        </span>
        <span class="menu menuBackground" id="iMenu4" onclick="ChangeFrame('paramconfig.asp',4)">
            <label id="laConfig" name="laConfig" class="mousepointer">Configuration</label>
        </span>
        <span class="logout" onclick="GoAway()">
            <label id="laExit" name="laExit" class="mousepointer">Logout</label>
        </span> 
    </div>

这会导致我要加载的页面和参数配置框架加载。但我不知道如何走得更远。在检查中我可以看到这段代码

<iframe frameborder="0" scrolling="no" id="contentframe" name="contentframe" class="contentframe" src="paramconfig.asp" style="height: 619px; width: 938px;"></iframe>

这让我觉得它是从 paramconfig.asp 文件生成的

接下来我想选择时间设置,这是一个我将鼠标悬停在上面的按钮,然后在显示的页面中单击,但以下选择器均不起作用:

page.click('text=Time Settings')
page.click('id=aTimeSettings')

同样奇怪的是,我无法在检查中使用右键单击和复制命令复制在检查中看到的代码。我在屏幕截图中看到了下面的代码,这就是我获取上面的文本和 ID 的方式。

我需要在 playwright 中做什么才能让它像我通过其网页以用户身份访问设备时一样单击时间设置按钮?

time setting html from inspect

【问题讨论】:

    标签: python playwright


    【解决方案1】:

    您不能将 page.click 用于位于不同页面(在 iframe 中)的元素。您需要get a handle on the frame 并与之互动。

    类似:

    frame = page.frame(name="frame-name")
    frame.click("the selector of the element you want to click inside the frame")
    

    the codegen feature of Playwright 可能会给你一些正确选择器的灵感等等。不过从文档来看,它生成的 JavaScript 似乎只是为了获得灵感。

    (P.S. 这里没有 Python 经验)

    【讨论】:

    • 谢谢,这给了我让它工作所需的提示。
    猜你喜欢
    • 2020-09-24
    • 1970-01-01
    • 2022-12-21
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 2021-06-25
    • 2022-12-22
    • 2021-10-16
    相关资源
    最近更新 更多