我一直在测试一个桌面应用程序(WPF)。使用: C#,Appium,WinAppDriver。一个菜单中有几个数字文本框。这里的问题是我无法访问这个页面上特定文本框的UpButton,因为所有的上/下按钮都有相同的ID "PART_IncreaseButton“。
它是一个内置向下控件的数字文本框。一份菜单里有几份。文本框
我使用inspect.exe来标识对象。检查器中的树:检查截图
因此,在自定义下,textbox的3个控件“编辑”、“按钮”、“按钮”和“"Root__Blue_AutomationId"”,我可以访问文本框,例如,在文本框中写入一些东西。
但是,如果我检查特定文本框的up按钮,它就有automationID automationID。并且其他文本框的上控件具有相同的ID。例如,只有AutomationID的rootID不同,并且is控件的ID保持不变:
根ID (文本框的):"Root__Blue_AutomationId" UpControl ID:"Part_Increasebutton"
根ID (第二个文本框,绿色通道不同):"Root__Green_AutomationId" UpControl ID:"Part_Increasebutton"
如何管理它来访问第二个文本框的UpControl?
发布于 2019-10-31 17:00:59
页面上的多个元素完全有可能具有相同的自动化id。
当这种情况发生时,可以做三件事来识别和定位单个元素。
如果您正在使用设计良好的应用程序,那么推荐的方法是选项3。它允许您使用通用逻辑与任何上/下按钮交互,同时通过包含它们的文本框唯一地标识它们。
发布于 2019-11-05 12:31:29
非常感谢你的支持。
我这样做的方式如下(两个步骤):
首先查找父元素:
Editor.FindElementByAccessibilityId("Root__Blue_AutomationId");WindowsElement TB1 =
然后找到其他控件:
TB1.FindElementByAccessibilityId("PART_IncreaseButton");AppiumWebElement TB1UpControl =
点击UpControl:
动作生成器=新动作(编辑器);
builder.Click(TB1UpControl).Perform();
诚挚的问候!
发布于 2019-11-05 14:01:54
这是我的实现。为了简单起见,我没有添加错误处理。代码编译,但我还没有测试这一点。向上/向下按钮和文本框包含在一个对象中,因此它与其他代码很好地分开。
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Windows;
using System.Collections.ObjectModel;
using System.Linq;
namespace Example
{
class SpinControl
{
public int Value {
get
{
//call _textBox here to get the value from your control.
return 0;
}
}
private readonly AppiumWebElement _increaseButton;
private readonly AppiumWebElement _decreaseButton;
private readonly AppiumWebElement _textBox;
public SpinControl(string automationID, WindowsDriver<WindowsElement> Driver)
{
WindowsElement customControl = Driver.FindElementByAccessibilityId(automationID);
ReadOnlyCollection<AppiumWebElement> customControlChildren = customControl.FindElementsByXPath(".//*");
_increaseButton = customControlChildren.First(e => e.FindElementByAccessibilityId("Part_IncreaseButton").Id == "Part_IncreaseButton");
_decreaseButton = customControlChildren.First(e => e.FindElementByAccessibilityId("Part_DecreaseButton").Id == "Part_DecreaseButton");
_textBox = customControlChildren.First(e => e != _increaseButton && e != _decreaseButton);
}
public void Increase()
{
//call _increaseButton here
}
public void Decrease()
{
//call _decreaseButton here
}
public void Input(int number)
{
//call _textBox here
}
}}
可以这样使用:
SpinControl sp = new SpinControl("customControlAutomationID", Driver);
sp.Increase();
sp.Decrease();
sp.Input(5);
int value = sp.Value;https://stackoverflow.com/questions/58628662
复制相似问题