0

I have a span with some id; and within that span, there is both a paragraph p tag and some text with no tags as follows. I am using selenium webdriver to target the street address below.

 <span id="myspan">
    <p>fullname</p>
    street address - has no tags attributes.
 </span>

I need to target the street address ONLY without the fullname.

I was thinking something like this:

span#myspan.not(p) // select all content of the span element, then ignore the p 
tag within it, so that only the street address is left. 

//

driver.findelement(By.cssSelector("span#myspan.not(p)")) // should select street 
address

Thanks again for your help.

Sitam Jana
  • 3,123
  • 2
  • 21
  • 40
pelican
  • 5,846
  • 9
  • 43
  • 67
  • As far as I know, you can't do that with CSS... have a look at [this](http://stackoverflow.com/q/5688712/2999215). However, with jQuery you can try something like `$("#myspan").contents(":not(p)").text();`. (**Edit:** Seems like that only works in some versions of jQuery.) – seiseises Mar 22 '14 at 03:11
  • Thanks seiseises, it looks like the only route here is to use what you said with the javascriptExecutor within selenium webdriver. But if not in css, what about using xpath maybe? Is there a way to accomplish above issue using xpath? thanks again – pelican Mar 22 '14 at 03:17
  • Sorry, never used selenium or xpath, I just saw the jQuery tag and had a try. Hope someone else can help you! – seiseises Mar 22 '14 at 03:24
  • That's ok, although Amith below, gave me the exact solution I was looking for, I will be using Javascript in tough situations where Selenium selectors aren't enough. For instance, using JavascriptExecutor(jqueryScript) to run javascript/jquery code within Java. So that's still very helpful, thanks. – pelican Mar 22 '14 at 16:59

1 Answers1

0

Well, as far as my knowledge goes, there is no selector or xpath that can do this directly. But if you are using java the following should get you the required text :

String spanText = driver.findElement(By.cssSelector("span#myspan")).getText();
String pText = driver.findElement(By.cssSelector("span#myspan > p")).getText();
String requiredText = spanText.replace(pText,"");

The first statement should get you fullname street address, second will get fullname and third statement will return street address.

If you want to remove leading and trailing white spaces, following should work :

System.out.println(requiredText.trim());
Amith
  • 6,818
  • 6
  • 34
  • 45