3

My source goes like this:

<div name="something" id="something"....>IPS125</div>

the number after IPS always changes. So I need to give the identification attribute as partial text "IPS". (name and id are horribly changing every time I open source.)

3

Use contains() or starts-with() function in XPath:

Solution :

With contains():

//*[contains(@id,'IPS')]

With starts-with():

//*[starts-with(@id,'IPS')]

Where * means any element. Let me know If any query.

2

You should use xpath. If your text 'IPS' is the text inside tag use this: //*[contains(.,'IPS')].

If 'IPS' is for example part of @class atrribute use this: //*[contains(@class,'IPS')].

If 'IPS' is always at the begginig of the text you can use starts-with command instead of contains.

Unfortunately web browsers dont support Xpath 2.0 which provides regular expressions.

  • After the edit the answer is quite simple. Use the first option from my answer: //div[contains(.,'IPS')] – kotoj Apr 28 '16 at 12:25
0

I use this function with good results:

    static WebElement getElementsWithAlteratingNames(String PartialName, String Tag, WebDriver driver, String Attribute) {
    try {
        List<WebElement> Elements = driver.findElements(By.tagName(Tag));
        String[] ElementStrings = new String[Elements.size()];
        System.out.println(Elements.size());
        for (int LoopCounter = 0; LoopCounter < Elements.size(); LoopCounter++) {
            ElementStrings[LoopCounter] = Elements.get(LoopCounter).getAttribute(Attribute).toString();
            if (ElementStrings[LoopCounter].contains(PartialName)) {
                System.out.println(ElementStrings[LoopCounter]);
                return Elements.get(LoopCounter);
            }
        }

    } catch (Exception e) {
        System.out.println("Could not generate a list of elements");
    }
    return null;
}

I've kept it fairly abstract so you can easily costumize it to serve the individual needs of your wanted Element.

Update

Since the Method is just not good and throws away efficiency if made a small update that does pretty much the same:

static WebElement findElem(String Tag, String Att, String Value){ 
    List<WebElement>Elems = driver.findElements(By.tagName(Tag)); 
       for(WebElement Elem : Elems){ 
           if(Elem.getAttribute(Att).contains(Value)) return Elem; 
       }
     } 

Since the code above isn't something that would pass a code review if one of my team did it I wouldn't feel good to leave it that way.

  • what is Elemente in Elemente.get(LoopCounter).getAttribute? If you would, include import statements. – Thufir Jul 13 '17 at 1:28
  • Elements, it should be Elements. I guess my german broke through. I'll edit it, thanks for the heads up. – Daniel Jul 13 '17 at 7:21
  • That method makes me dizzy, lol. – FDM Jul 13 '17 at 8:24
  • That's because, looking at it now, it's just not good. static WebElement findElem(String Tag, String Att, String Value){ List<WebElement>Elems = driver.findElements(By.tagName(Tag)); for(WebElement Elem : Elems){ if(Elem.getAttribute(Att).contains(Value)) return Elem; } } is much more concise and leaves out all the useless boilerplate. – Daniel Jul 13 '17 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.