How can I get all the content in a div element then strip out a table element leaving only what elements I want?

27 views
Skip to first unread message

Super Kevy

unread,
Dec 4, 2015, 4:17:01 PM12/4/15
to Watir General
How can I get all the content in a div element then strip out a table element leaving only what elements I want?

Assume  a structure like
<div id="contents>
<p>Static text I want to offline into a file as a static comparison later</p>
<p>If the static content is updated by a change request I will detect the changes by a file compare</p>
<table><tr><td>this table is dynamic is updated with new values all the time</td></tr><t/able>
<p>More static content</p>
<ul><li>etc, etc</li></ul>
</div>

 The notion is I store the statics in a file then iterating a test that can compare the current static elements to an offline file and let me know if there are changes.
 I would like to be able to strip the table from the div wrapper and just see the whats left for the comparison.


Derek W

unread,
Dec 4, 2015, 4:31:43 PM12/4/15
to watir-general
What is the context?  Are you first getting the div via xpath and storing it in a variable?

If so, you can just do a regex using something like this right? "<table>[0-9a-zA-Z<> \/]*<\/table>"

If you're trying to do this all in 1 xpath locator, can't you do something like:

//div[@id="contents"]//table//text()



--
--
Before posting, please read http://watir.com/support. In short: search before you ask, be nice.
 
watir-...@googlegroups.com
http://groups.google.com/group/watir-general
watir-genera...@googlegroups.com

---
You received this message because you are subscribed to the Google Groups "Watir General" group.
To unsubscribe from this group and stop receiving emails from it, send an email to watir-genera...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Derek Wrobel
425-243-3355

Kevin Petry

unread,
Dec 4, 2015, 4:43:53 PM12/4/15
to watir-...@googlegroups.com
Thanks for the hint on regex.  I'll post my code when it's ready so the group can learn from the experience.

You received this message because you are subscribed to a topic in the Google Groups "Watir General" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/watir-general/U_fq01pvWZo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to watir-genera...@googlegroups.com.

John Fitisoff

unread,
Dec 7, 2015, 11:04:22 PM12/7/15
to watir-...@googlegroups.com
Not sure if this fits your problem, but one approach would be to look at the output of b.div(:id, 'contents').text. It may be possible to use regexp matching to get the text leading up to the div and then the text after, without having to worry specifically about what element outside the table the text is stored in. For example, maybe the table has a static header row and you know that the first 2 column row names are going to be foo and bar. You could then write a regexp to get the text above the table. And then use a similar strategy to get the text below. Then you don't have to worry about dynamic table updates in between checks for the text values of elements within the div. If you see some reliable pattern marking the beginning and end of the table text then this should work. 

Alternative idea is to get the enclosing div HTML, use a regexp to strip out the table tags and everything between and then feed the resulting string to Nokogiri for further manipulation (either convert remainder to text or traverse the remaining elements one by one.) Seems sort of low rent but that would definitely work.


From: Derek W <derek...@gmail.com>
To: watir-general <watir-...@googlegroups.com>
Sent: Friday, December 4, 2015 1:31 PM
Subject: Re: [wtr-general] How can I get all the content in a div element then strip out a table element leaving only what elements I want?
Reply all
Reply to author
Forward
0 new messages