java - WinAppDriver 如何在 Java 中定义 WindowsElement 而不会出现 java.lang.ClassCastException

标签 java winappdriver

我想定义一个 WindowsElement,以便可以重用它,但如果运行它,它会抛出 java.lang.ClassCastException: org.openqa.selenium.remote.RemoteWebElement 无法转换为 io.appium.java_client.windows。 Windows元素

我发现了一些 WinAppDriver 错误,但所有错误都已关闭,没有任何有用的信息。

public class WinAppDriverWaitsExample {

    private static WindowsDriver<WebElement> driver = null;
    private static WebElement alarm = null;

    @BeforeClass
    public static void setup() {
        try {       
            DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("app", "Microsoft.WindowsAlarms_8wekyb3d8bbwe!App");             

            driver = new WindowsDriver<WebElement>(new URL("http://127.0.0.1:4723"), capabilities);
            driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);

            //To make sure app is launched
            alarm = driver.findElementByName("Alarm tab");
            Assert.assertNotNull(alarm);

        }catch(Exception e){
            e.printStackTrace();
        } finally {
        }
    }

    @Test
    public void timer() {
        System.out.println("Start and stop the alarm clock");
        //It is AutomationID
         driver.findElementByAccessibilityId("TimerPivotItem").click();
        WindowsElement  startBtn= (WindowsElement) driver.findElementByName("Start");
         WindowsElement  pasueBtn=(WindowsElement) driver.findElementByName("Pause");
         startBtn.click();
         pasueBtn.click();

    }

最佳答案

将元素类型更改为 WebElement 解决了我的问题。

@Test
    public void timer() {
        System.out.println("Start and stop the alarm clock");
        //It is AutomationID
         driver.findElementByAccessibilityId("TimerPivotItem").click();
         WebElement  startBtn= driver.findElementByName("Start");
         WebElement  pasueBtn=driver.findElementByName("Pause");
         startBtn.click();
         pasueBtn.click();

    }

关于java - WinAppDriver 如何在 Java 中定义 WindowsElement 而不会出现 java.lang.ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60351727/

相关文章:

java - 使用非 Sun/Oracle JVM 的经验?

java - Linux下Java的虚拟内存使用,使用了太多内存

java - 更新 JFreeChart 上的数据

c# - 我怎样才能知道元素是否不可见

visual-studio - Appium WinAppDriver C# 计算器示例错误

selenium - Appium/WinAppDriver 无法找到上下文菜单 - 但仅在某些机器上

java - FFT 吉他调音器应用程序 - 频率不正确

java - 如何将所有值设置回原始起始值

c# - Selenium 点击错误的元素