【问题标题】:How do I click a menu item from the application menu using Appium and Python on Windows?如何在 Windows 上使用 Appium 和 Python 从应用程序菜单中单击菜单项?
【发布时间】:2019-09-27 12:09:17
【问题描述】:

我正在为应用程序自动进行 UI 测试,但在 Windows 上的菜单项遇到问题。 Fwiw,我让它在 Mac 上为姐妹应用程序工作。 我正在使用 Python 中的 Appium。

我可以使用 Inspect.exe 找到菜单树,单击顶级菜单,然后打开下拉菜单,在这里我找到菜单项,我想单击,但 WinAppDriver 失败并出现以下错误: {"status":105,"value":{"error":"element not interactable","message":"An element command could not be completed because the element is not pointer- or keyboard interactable."}}

下面的python重现了这个问题。

import time
import unittest
from appium import webdriver

app_exe_path = "C:\\Program Files\\Phase One\\Capture One 12\\CaptureOne.exe"
menu_name = "Select"
menu_item_name = "First"
switch_window = True
# app_exe_path = "C:\\Windows\\Notepad.exe"
# menu_name = "File"
# menu_item_name = "Open..."
# switch_window = False


class ClickApplicationMenuItem(unittest.TestCase):
    def test_click_application_menu_item(self):
        driver = webdriver.Remote(
            command_executor="http://localhost:4723",
            desired_capabilities={"app": app_exe_path},
        )
        if switch_window:
            time.sleep(5) # non-optimal code for the sake of a simple repro
            handles = driver.window_handles
            driver.switch_to.window(handles[0])
        menu = driver.find_element_by_name(menu_name)
        menu.click() # fails in the Notepad case
        item = menu.find_element_by_name(menu_item_name)
        item.click() # fails in the CaptureOne case


if __name__ == "__main__":
    unittest.main()

关于如何点击菜单项的任何建议?

【问题讨论】:

    标签: python-appium winappdriver


    【解决方案1】:

    这是菜单项的最终结果(我保留menu.click(),因为它适用于应用程序,我正在测试):

       from selenium.webdriver.common.action_chains import ActionChains
       actions = ActionChains(driver)
       actions.click(item)
       actions.perform()
    

    【讨论】:

    • 所以你根本不需要那些坐标?那更好:-)。
    • 完全正确 :-) 让我有点困惑的是为什么 WebElement.click 方法报告说同一个元素不可点击。也许有一天有人会深入研究并提出问题......
    【解决方案2】:

    由于您能够找到这些元素,我假设您可以访问它们的属性。 一个简单的解决方法是单击元素坐标而不是单击元素本身。通常,单击坐标不是一个好主意,但由于您从元素本身获取坐标,我认为这里没有问题。

    试试这样的:

    menu = driver.find_element_by_name(menu_name)
    driver.Mouse.Click(menu.coordinates)
    item = menu.find_element_by_name(menu_item_name)
    driver.Mouse.Click(item.coordinates)
    

    我确实收到了一条警告,指出鼠标功能已过时,应该使用 ActionsActionBuilder 类。您也可以探索这些选项,但我在 winappdriver 的 github 页面上发现了一个问题,该问题已于 2018 年 3 月关闭,关于 Actions 类。目前尚不清楚它为什么关闭。您可以找到另一种点击坐标的方法。

    资源: Actions issue

    【讨论】:

    • 谢谢,这是一个优雅的提议 :-) 下次我在 21 日进行该项目时,我会尝试一下
    • 太棒了!一定要回来告诉我它是如何工作的,我还没有实施这个解决方案,所以不确定它是否能开箱即用。
    • 感谢@PixelPlex 让我走上正轨!我刚刚发布了我自己的答案:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-22
    • 2016-03-23
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多