简体   繁体   中英

I need to find second element with the same automationid by Xpath

I'm struggling with finding by Xpath. My problem is that the software that I'm testing has one specific function where I need to scan two components (is it called two-step scan) and there are two textboxes without the name and with the same Automationid. So I need to find the second one I tried this but it does not work.

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

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

I'm using winium and I'm testing WPF application.

You really need to use a FindsBy ? We are unable to create a correct XPath to a FindsBy to these buttons without the html. This way, I only can provide you another solution, and update my awnser when the html code be provided.

You can use FindElement , the plural one, that is able to return both. After, you select the desired button by index. A example method able to do it:

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);  
}

Update locators as described here:

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

The difference is that in my case (locator)[n] you select n-th element out of all elements found by a locator. And by locator[n] you search for element that has n-th positions inside parent nodes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM