【问题标题】:I need to find second element with the same automationid by Xpath我需要通过 Xpath 找到具有相同自动化 ID 的第二个元素
【发布时间】:2018-05-29 17:07:26
【问题描述】:

我正在努力寻找 Xpath。我的问题是我正在测试的软件有一个特定的功能,我需要扫描两个组件(是否称为两步扫描),并且有两个没有名称且具有相同 Automationid 的文本框。所以我需要找到我尝试过的第二个,但它不起作用。

[FindsBy(How = How.Xpath, Using = "//*[@AutomationId='ScanTextBox'][1]")]
public IWebElement ScanTextBox1;

[FindsBy(How = How.Xpath, Using = "//*[@AutomationId='ScanTextBox'][2]")]
public IWebElement ScanTextBox2;

我正在使用 winium,我正在测试 WPF 应用程序。

【问题讨论】:

  • 您看到 (How = How.Xpath, Using = "//*[@AutomationId='ScanTextBox'][2]") 有什么错误?可以更新相关的HTML吗?

标签: c# wpf selenium testing xpath


【解决方案1】:

按照此处所述更新定位器:

(//*[@AutomationId='ScanTextBox'])[1]
(//*[@AutomationId='ScanTextBox'])[2]

不同之处在于,在我的情况下,(locator)[n]您从定位器找到的所有元素中选择 n-th 元素。通过locator[n] 搜索在父节点内具有n-th 位置的元素

【讨论】:

  • 显然这应该钉住OP
【解决方案2】:

你真的需要使用FindsBy吗?如果没有 html,我们无法为这些按钮创建正确的 XPath 到 FindsBy。这样,我只能为您提供另一种解决方案,并在提供 html 代码时更新我的​​ awnser。

您可以使用FindElement,它是复数形式,可以同时返回。之后,您按索引选择所需的按钮。能够做到这一点的示例方法:

public IWebElement GetScanTextBox(int index)
{
    return Driver
        .FindElements(By.XPath("//*[@AutomationId='ScanTextBox']"))
        .ElementAt(index);
}

public void UsageExample()
{
    var buttonOne = GetScanTextBox(0);
    var buttonTwo = GetScanTextBox(1);  
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多