How to getText() from nested elements

405 views
Skip to first unread message

Faiz

unread,
Oct 26, 2013, 1:33:38 PM10/26/13
to seleniu...@googlegroups.com
Hi,


How do i getText() for p element. i want the text "4026-username already in use"

<img id="warningImageContainer" height="30px" style="vertical-align: middle; background-color: transparent;" src="/lop-test/images/error.png" alt="">
<p style="display:inline;">4026-username already in use</p>

Please let me know if any one has any solution for this.

Any help is appreciated.

Thanks
Faiz.

David

unread,
Oct 27, 2013, 3:33:22 PM10/27/13
to seleniu...@googlegroups.com
What's your locator string first of all?

Ibrahim Faiz

unread,
Oct 27, 2013, 4:36:09 PM10/27/13
to seleniu...@googlegroups.com
Hi David,

There's isn't any particular locator string that is working for this right now for the nested element 'p' from where I'd want to extract the text and place a condition on that basis. 

Please let me know if you got anything on this
--
You received this message because you are subscribed to a topic in the Google Groups "Selenium Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/selenium-users/c3mPjsutsBI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/6d10f5ea-11eb-463d-b0f0-f9006b1d9fa3%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


--
Thanks,
Ibrahim Faiz.

Ibrahim Faiz

unread,
Oct 28, 2013, 1:50:09 AM10/28/13
to seleniu...@googlegroups.com
I am unable to construct a locator for the element p actually..

Can any please provide some input on this.
--
Thanks,
Ibrahim Faiz.

David

unread,
Oct 28, 2013, 10:00:45 PM10/28/13
to seleniu...@googlegroups.com
Try

getText() on the following locators:

For XPath: id('warningImageContainer')/following-sibling::p

For CSS: img.warningImageContainer + p

Now, if you have multiple elements that match those locators (when tested via FirePath for Firefox, etc.) then you have a separate problem on narrowing down to the desired element. And a complete dump of an example page source would be helpful as the current HTML is too sparse to tell if locator will work well or not.

And if you know the specific text you're looking for already or at least part of that text (a text pattern), you can do something like this in XPath:

//p[contains(text(),'4026-username already in use')]

which will match if such error occurs, and you can simply do a verify that element exists and not even have to do getText(), unless you need the text. Now, if 4026 was dynamic/changing, then you can omit it in the contains matching above and it will still match and getText() will then give you back text with the 4026 in it.

On Saturday, October 26, 2013 10:33:38 AM UTC-7, Faiz wrote:
Reply all
Reply to author
Forward
0 new messages