首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >弹出确认窗口的Web自动化

弹出确认窗口的Web自动化
EN

Stack Overflow用户
提问于 2021-11-09 09:12:56
回答 1查看 272关注 0票数 0

与其说这是一个代码问题,不如说是一个自动化问题。

有一个桌面网站由iframes(在我看来,那些是iframes),这是某种B2B商店。所有的网站元素都是固定的,它们在屏幕之间移动时不会移动或改变。

我需要的是自动点击几个按钮,直到我来确认,其中一个点击是一个弹出式窗口与确认/取消。弹出后,下一个屏幕打开,再点击一个按钮,这对我来说是手工操作。

我尝试过使用几个QA测试软件,比如Selenium来实现自动化,但这并没有帮助,当我谈到弹出式时,它就停止了。在我看来,软件似乎没有意识到弹出式覆盖,尽管弹出总是一样的。

有没有人知道如何克服上述问题?

提前谢谢大家。

重要:弹出窗口是网站的一部分.

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-09 13:45:00

如果操作系统是Windows,并且弹出是一个本地Windows对话框,而不是Selenium IDE访问的网站的一部分,那么您可以尝试通过调用Windows来解决问题。不确定如何使用selenium.ide实现这一点,但可以使用用支持的编程语言之一编写的常规selenium.ide代码来实现,这些代码能够执行本机Windows函数。

例如,这里是一个独立的Java类,它启动Chrome,导航到一个网站,然后单击本机对话框“警告”中显示的OK按钮。

代码语言:javascript
复制
package example;

import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
import org.openqa.selenium.chrome.ChromeDriver;

import java.time.Duration;

public class Main {
    public static void main(String... args) {
        ChromeDriver driver = new ChromeDriver();
        driver.get("https://google.com");

        Pointer dialog = getNativeDialog("Warning");
        Pointer button = getNativeButton(dialog, "OK");
        click(button);
    }

    /**
     * Returns a handle to a dialog window by its name
     * @param dialogTitle exact name of the window to find
     */
    public static Pointer getNativeDialog(String dialogTitle) {
        final String DIALOG_CLASS = "#32770";
        return User32.INSTANCE.FindWindowEx(null, null, DIALOG_CLASS, dialogTitle);
    }

    /**
     * Returns a handle to the button with specific label within a parentWindow
     * @param parentWindow handle to a window, obtained e.g. with {@link #getNativeDialog(String)}
     * @param buttonLabel label of the button to search by
     */
    public static Pointer getNativeButton(Pointer parentWindow, String buttonLabel) {
        final String BUTTON_CLASS = "Button";
        return User32.INSTANCE.FindWindowEx(parentWindow, null, BUTTON_CLASS, buttonLabel);
    }

    /**
     * Sends BM_CLICK message to a window (or other control) which is equivalent of clicking it
     * with primary mouse button
     * @param target an input handle obtained with e.g. {@link #getNativeButton(Pointer, String)}
     */
    public static void click(Pointer target) {
        final int BM_CLICK = 0x00F5;
        User32.INSTANCE.SendMessage(target, BM_CLICK, null, null);
        sleep(Duration.ofMillis(500));
    }


    private static void sleep(Duration duration) {
        try {
            Thread.sleep(duration.toMillis());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private interface User32 extends StdCallLibrary {
        User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class, W32APIOptions.DEFAULT_OPTIONS);

        /**
         * Finds window (or other type of control, specified by lpszClass).
         * See https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-findwindowexw
         */
        Pointer FindWindowEx(Pointer hWndParent, Pointer hWndChildAfter, String lpszClass, String lpszWindow);

        /**
         * Sends message to a window (or other type of control). Can be used to simulate user input.
         * Note when the result of sending message triggers modal dialog to show, this method may block indefinitely,
         * unless the dialog is handled in separate thread.
         * See https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendmessage
         */
        Pointer SendMessage(Pointer hWnd, int Msg, String wParam, String lParam);
    }
}

这个特殊的例子需要这些库

代码语言:javascript
复制
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna</artifactId>
            <version>3.4.0</version>
        </dependency>
        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>platform</artifactId>
            <version>3.4.0</version>
        </dependency>
    </dependencies>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69895478

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档