RE: How to handle these dynamic id elements “ext-gen216”, “ext-comp216 ” using Xpath ? When to use Starts-with,Contains keywords and text ?Which kind of context we use those keywords?

How to handle these dynamic id elements “ext-gen216”, “ext-comp216 ” using Xpath ? When to use Starts-with,Contains keywords and text ?Which kind of context we use those keywords?

Raghul Default Asked on September 25, 2014 in Selenium WebDriver.

Hi,

can you give us the URL of the site? Its will be good to give accurate answer.

Thanks
Virender

on September 25, 2014.

If your query is resolved then please accept the answer.

on September 27, 2014.
Add Comment
3 Answers

Hi Ragul,

Can you please post the HTML here?

Generally you use starts-with, contains and text keywords, when you want to compare some expected string with your string.

Thanks,
Vishal

Doctor Answered on September 25, 2014.

@Vishaalcbe: These are mainly used to identify an element/Object, when you do not have any other identifiers (Ex: ID, ClassName, etc..)..And as per your answer I am not sure whether we can use these also for String matching,Ā  could you please post the code (compare some expected string with your string.) then it will be helpful for me to understand that we can use starts-with, contains for string matching also.Please correct me if I am wrong.

on September 26, 2014.

Hi Sachin,

Expected = The

String validation = driver.findElement(By.xpath(“XXXX”)).getText();
if (validation.startsWith(“The”)) {
driver.findElement(By.xpath(“YYYY”)).click();
} else {
System.out.println(“Try again buddy”);
}

String validation = driver.findElement(By.xpath(“XXXX”)).getText();
if (validation.contains(“The”)) {
driver.findElement(By.xpath(“YYYY”)).click();
} else {
System.out.println(“Try again buddy”);
}

on September 26, 2014.

Hi Vishaalcbe,Thanks for the post (It was really good), but I know this šŸ™‚ and as per your answer “Generally you use starts-with, contains and text keywords, when you want to compare some expected string with your string.” where you are storing “starts-with, contains” in a strings (In above code)… I was bit confused here, “starts-with, contains” are used to store in a string and to verify text? (As per you). I thought It’s just to identifying an element/object we use. Please correct me.If my understanding is wrong then below answer also wrong.http://forumsqa.com/answer/re-how-to-handle-dynamically-changing-element-in-web-page/

on September 26, 2014.

@Sachin.

It’s great that you already knew this, but this would help others who might not know this. I was speaking from String comparison perspective of Java and not from element perspective. It helps from Xpath perspective as well.

So the bottom line is that it can be used for both String comparison and find an element using Xpath.

Cheers,
Vishal

on September 26, 2014.

(b)

on September 26, 2014.

@Vishaalcbe, thanks and the question was what is the use of “starts-with, contains” and String comparison is different thing here (My understanding). As you said String comparison, so I want to know how to work on “starts-with, contains” for string matching? (I don’t know so).What is my understanding is “starts-with, contains” both are used in xpath to identify an element/object and and after identifying theĀ element/object we can use the string for matching theĀ element/object text. I was not knowing these are also use in string comparison?. Could you please show me how to do it please? (I am also learner).Ya, what you said I 100% agree with that “others will learn from” this but we need to give proper solution :).

on September 27, 2014.

@Sachin, Yes your understanding is correct. The question is how to identify dynamic id elements so I feel your suggestion is good to work around. After Identifying the element then only we do String comparison, actions, etc.. and starts -with and contains both are related to xpath and these are not using to compare strings for (We need to compare object string (Text, etc) but not object text (string) and xpath string.)Ā  Ex: String expected = “//sapn[starts-with(@id, ‘ext-‘)]” (This is not way to do string comparison I feel);Ā  Thanks for the nice discussion sachin and vishaal.

on September 27, 2014.

@Rocky, Thanks for detailed comment. Now saying bye to this discussion šŸ™‚ because no use of further discussion. Happy coding.

on September 27, 2014.

@Sachin

starts-with and contains are also used in Java for comparison/matching.

For your question above “As you said String comparison, so I want to know how to work on ā€œstarts-with, containsā€ for string matching? (I donā€™t know so).” From learning perspective – everybody is a learner.

An easy solution…

List<String> days = Arrays.asList(“SUN”, “MON”, “TUE”, “WED”, “THU”, “FRI”, “SAT”);
String day = newStr4.substring(0, 3).toUpperCase();
if (days.contains(day)) {
// …
}

I just gave my perspective and it is widely available on the net.

on September 29, 2014.

@Vishal,The question is related to xpath and you are saying from string point of you. Not sure why you are not accepting this.

on October 3, 2014.

@Rocky

I’ve already accepted the answer on SEP -26. You can find it above. Here I’m trying to answer a question on the other format of starts-with, contains.

on October 7, 2014.

@Vishal,

Please stop this discussion.

on October 9, 2014.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.