how to select text nodes preceding and following break tags ?

6,547 views
Skip to first unread message

Joeyjoey

unread,
Nov 22, 2010, 5:40:31 PM11/22/10
to webdriver
how can I select text nodes preceding and following <br> or <br/>
tags ?

Joeyjoey

unread,
Nov 22, 2010, 5:44:46 PM11/22/10
to webdriver
for example
<html>
<div>
text1
<br>
text2
<br/>
text3
<br/>
text4
<br>
text5
</div>
</html>

Need webdriver to select each text nodes....

Tan

unread,
Nov 22, 2010, 5:51:42 PM11/22/10
to webdriver
Are you looking at something like this

driver.findElements(By.xpath("//br/preceding::text()"));

Cheers,
Tan

Joeyjoey

unread,
Nov 22, 2010, 5:57:14 PM11/22/10
to webdriver
yeah for example, I am trying to extract teh PostingID

http://losangeles.craigslist.org/wst/roo/2074898121.html

that xpath doesn't seem to work.....

Joeyjoey

unread,
Nov 22, 2010, 5:58:03 PM11/22/10
to webdriver
here's my code

public static void main(String[] args) {
// TODO Auto-generated method stub
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("javascript.enabled", false);
//initialize browser
WebDriver driver = new FirefoxDriver(profile);

driver.get("http://losangeles.craigslist.org/wst/roo/
2074898121.html");
System.out.println(driver.findElement(By.xpath("//br/
preceding::text()[1]")).getText());
}

Tan

unread,
Nov 22, 2010, 6:18:40 PM11/22/10
to webdriver
Hi Joey

Having a look at the page, could you use the div with id='userbody'
rather than br tag? Using br tag lead to more fragile test in this
case.

As much as I hate XPath, this one might helps

//div[@id='userbody']/following::text()[1]

Cheers,
Tan

Tan

unread,
Nov 22, 2010, 7:01:29 PM11/22/10
to webdriver
Another alternative is

StringUtils.substringBetween(getDriver().findElement(By.tagName("body")).getText(),"PostingID:","\n")

We can avoid using complicated XPath

Cheers,
Tan

doridori Jo

unread,
Nov 22, 2010, 10:23:17 PM11/22/10
to webd...@googlegroups.com
actually, I need an xpath that represents an individual text node. Otherwise, I have to wrap all the text nodes with an element, and select this element.....I am afraid this will create overhead when running tests.

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.


Simon Stewart

unread,
Nov 23, 2010, 4:48:47 AM11/23/10
to webd...@googlegroups.com
You could just use "findElement(By.tagname("br"))" to find the breaks
and then use the JavascriptExecutor to get the text content of the
preceding nodes: you don't need to be using xpath for this. Although I
can't think of the exact code, something like:

String text = (String) ((JavascriptExecutor) driver).executeScript(
"return arguments[0].parent.nodeValue",
element);

Should point you in the right direction.

Simon

Reply all
Reply to author
Forward
0 new messages