Domanda

I have a webpage where there are few text fields to be filled up with some values using Selenium WebDriver. But I'm unable to fetch these items using the APIs provided in selenium framwework because the IDs are changing dynamically.

For example one of the text fields has the following ID

<input id="order_unit_line_rate_806099_unit_price" type="text" value="" uniqueattr="Dynamic Site Accelerator / Dynamic Site Accelerator / Platform Fee / / Price" size="10" onchange="javascript:numberFormatValid('order_unit_line_rate_806099_unit_price', parseI18nNumber($('order_unit_line_rate_806099_unit_price').value, ',', '.'), 5, '.');" name="order_unit_line_rate[806099][unit_price]">

where the digits 806099 which is a part of the id varies for every new page and every new text box. I didn't have any luck by using the findElements(By.id()) API.

can someone please suggest any approach for identifying these elements.

I'm using eclipse as the editor, java as the client driver and selenium webDriver as the automation framework.

Thanks Kiran

È stato utile?

Soluzione

You can use findElementsByTagName and then loop over each returned element, checking its ID versus your regular expression:

public static WebElement findElementByRegexId(FindsByTagName ctx, String tagName, String regex) {
    List<WebElement> l = ctx.findElementsByTagName(tagName);
    for (WebElement e : l) {
        if (e.getAttribute("id").matches(regex)) {
            return e;
        }
    }
    return null;
}

Altri suggerimenti

The selenium API also allows you to pass XPath locators, so you can probably try something like

findElements(By.xpath("//input[@uniqueattr=\"Dynamic Site Accelerator / Dynamic Site Accelerator / Platform Fee / / Price\"]"));

See here for more details.

Instead of using regular expression you can use below solution

//input[contains(@name, 'order_unit_line_rate')] 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top