Re: Is there any way to assert line break sentences in selenium web driver.

8,703 views
Skip to first unread message

Tarun Kumar

unread,
Jun 26, 2012, 1:06:29 AM6/26/12
to seleniu...@googlegroups.com
What text do you get when you use - ui-message-error-detail as element locator?
If you get extra spaces then you can trim them using xPath function normalize-space()


On Tuesday, June 26, 2012 1:00:13 AM UTC+5:30, Nax143 wrote:
Hi,
I have to assert the expected error message with actual error message . But the error message in HTML source page has line break.

I have used assert like this :

assertEquals("The email is blank. Please enter an email!",driver.findElement(By.xpath(XXXXXXXX)).gettext());


Page Source : 


<span class="ui-message-error-detail">The email is blank.<br/>Please enter an email!</span>
                                
note : If there is no line break tags , I can assert it. But due to line break I couldn't do it..script failing.

Can anyone help me..

Nax143

sai kiran nukala

unread,
Jun 26, 2012, 1:12:40 AM6/26/12
to seleniu...@googlegroups.com
@Tarun,normalize-space() replaces leading and trailing spaces. But in the Span's text it has <br/> in the middle of the text. 

@Nax, you have to use new line character while asserting the text. For example, the below snippet should work -

assertEquals("The email is blank.\nPlease enter an email!",driver.findElement(By.xpath(XXXXXXXX)).gettext());

Tarun Kumar

unread,
Jun 26, 2012, 1:16:31 AM6/26/12
to seleniu...@googlegroups.com
@sai -


<quote>The normalize-space function returns the argument string with whitespace normalized by stripping leading and trailing whitespace and replacing sequences of whitespace characters by a single space. </quote>

Lutfi Dughman

unread,
Jun 26, 2012, 11:19:54 AM6/26/12
to seleniu...@googlegroups.com
here is how you work around the br


//SPAN[.='The email is blank.Please enter an email!']

<span class="ui-message-error-detail">The email is blank.<br/>Please enter an email!</span>


.. now obviously this xpath doesn't tell whether there  is a BR in the span or not, but at elast it allow you to assert the content.

note there is no tspace between the '.' and work 'please'




--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/xt9xq6dzWvsJ.

For more options, visit https://groups.google.com/groups/opt_out.

Mark Collin

unread,
Jun 26, 2012, 11:27:40 AM6/26/12
to seleniu...@googlegroups.com

You should also be able to get the text in two distinct blocks:

 

//span/text()[1]

//span/text()[2]

Nax143

unread,
Jun 26, 2012, 12:46:09 PM6/26/12
to seleniu...@googlegroups.com
Thanks Tarun.
' normalize-space ' function worked

I have used path with normalise-space = //*[normalize-space(@id)='user-msg']/span[2]

Thanks once again for all for replies..

Nax143

unread,
Jun 26, 2012, 12:47:46 PM6/26/12
to seleniu...@googlegroups.com
Thanks Tarun.
' normalize-space ' function worked

I have used path with normalise-space = //*[normalize-space(@id)='user-msg']/span[2]

Thanks once again for all for replies..


Nax143

unread,
Jun 26, 2012, 1:12:33 PM6/26/12
to seleniu...@googlegroups.com
Sorry Tarun.. It didn't worked. I was mistaken.

My path is //*[@id='user-msg']/span[2]
How can I use the 'normalise-space' function? 

Page source is :
<div id="user-msg" class="ui-message-error ui-widget ui-corner-all"><span class="ui-message-error-icon"></span><span class="ui-message-error-detail">The email is blank.<br/>Please enter an email!</span></div>

Please send me the solution ...

Nax143

Tarun Kumar

unread,
Jun 26, 2012, 1:31:19 PM6/26/12
to seleniu...@googlegroups.com
Since there is </br> tag I am having a second thought about normalize-space being of no use in your case.
Did you try lutfijd  and Mark's solution?

Nax143

unread,
Jun 26, 2012, 2:07:28 PM6/26/12
to seleniu...@googlegroups.com
When I tried lutfijd solution , I am getting following error:
The given selector //SPAN[='The email is blank.Please enter an email!'] is either invalid or does not result in a WebElement. The following error occurred:
     [java] [InvalidSelectorError] Unable to locate an element with the xpath expression //SPAN[='The email is blank.Please enter an email!'] because of the following error:
     [java] [Exception... "The expression is not a legal expression."  code: "51" nsresult: "0x805b0033 (NS_ERROR_DOM_INVALID_EXPRESSION_ERR)"  location: "file:///var/folders/km/95m9f3rs28jfp_649qzlvsl40000gn/T/anonymous5457794172896486930webdriver-profile/extensions/fxdr...@googlecode.com/components/driver_component.js Line: 2621"]

When I tried Mark solution , I am getting following error:
code i have tried :
//*[@id='user-msg']/span[2]/text()[1]"

 str1=driver.findElement(By.xpath("//[@id='user-msg']/span[2]/text()[1]")).getText();


str2 =driver.findElement(By.xpath("//[@id='pass-msg']span[2]/text()[2]")).getText();

assertEquals("The email is blank. Please enter an email!",str1+" "+str2);

Error :
The given selector //*[@id='user-msg']/span[2]/text()[1] is either invalid or does not result in a WebElement. The following error occurred:
     [java] [InvalidSelectorError] The result of the xpath expression "//*[@id='user-msg']/span[2]/text()[1]" is: [object Text]. It should be an element.

Guys, any other solutions or correct me if u have any other idea.

    

Lutfi Dughman

unread,
Jun 26, 2012, 2:57:16 PM6/26/12
to seleniu...@googlegroups.com
here is your problem:

you forgot the '.' before the equal sign




//SPAN[.='The email is blank.Please enter an email!']

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/lflg4zxHpUQJ.

Mark Collin

unread,
Jun 26, 2012, 3:43:34 PM6/26/12
to seleniu...@googlegroups.com

Hmm, that’s valid XPath, looks like we may have a bug.

 

I’ll double check later today.

--

Mark Collin

unread,
Jun 26, 2012, 6:21:09 PM6/26/12
to seleniu...@googlegroups.com

It would seem I’m talking rubbish (obviously too late for me).  I got as far as raising it as:

 

http://code.google.com/p/selenium/issues/detail?id=4158

 

But then stepped back and had another good look at the error message and thought, “I’m being an idiot, did I ever really do this in WebDriver?  Or was it something I’ve done with Selenium RC a long time ago”.

 

And confirmed I’m talking rubbish on IRC:

 

<Ardesco> Hmm am I raising balls?

<Ardesco> http://code.google.com/p/selenium/issues/detail?id=4158

<Ardesco> I'm sure I used to be able to get WebElements this way, but I have a nasty feeling I'm getting my Selenium RC API locator startegies mixed up with WebDriver ones

<Dude-X> i never liked using text() in my xpath

<jarib> Ardesco: AFAIK using findElement() to find text nodes has never worked

<Dude-X> it works sometimes...

<Dude-X> actually hmm

<Dude-X> i always remove it when i find it :P

<Ardesco> I thought I had use it in the past, but now i'm doubting myself

<Ardesco> *used

<Ardesco> and I agree it's something to avoid if possible

<Dude-X> anyway use getText() on the element

<jarib> either improve the markup or just do getText().split("\n") on the span

<barancev> should wdSession.prototype.getWindow return the top window?

<Ardesco> feel free to close it

<Ardesco> and slap a muppet stamp on me

<Ardesco> :)

 

Should have confirmed it on IRC first really, silly me.

Nax143

unread,
Jun 27, 2012, 5:38:12 AM6/27/12
to seleniu...@googlegroups.com
Hi lutfijd,

I tried with '.' before equal sign , but no use. throwing error.

Mark,
I tried split , but no use. 

Regards,
Venkat


On Tuesday, 26 June 2012 19:57:16 UTC+1, lutfijd wrote:
here is your problem:

you forgot the '.' before the equal sign




//SPAN[.='The email is blank.Please enter an email!']
On Tue, Jun 26, 2012 at 2:07 PM, Nax143 <venkat...@adfonic.com> wrote:
When I tried lutfijd solution , I am getting following error:
The given selector //SPAN[='The email is blank.Please enter an email!'] is either invalid or does not result in a WebElement. The following error occurred:
     [java] [InvalidSelectorError] Unable to locate an element with the xpath expression //SPAN[='The email is blank.Please enter an email!'] because of the following error:
     [java] [Exception... "The expression is not a legal expression."  code: "51" nsresult: "0x805b0033 (NS_ERROR_DOM_INVALID_EXPRESSION_ERR)"  location: "file:///var/folders/km/95m9f3rs28jfp_649qzlvsl40000gn/T/anonymous5457794172896486930webdriver-profile/extensions/fxdri...@googlecode.com/components/driver_component.js Line: 2621"]
To post to this group, send email to selenium-users@googlegroups.com.
To unsubscribe from this group, send email to selenium-users+unsubscribe@googlegroups.com.

Nax143

unread,
Jun 27, 2012, 6:20:33 AM6/27/12
to seleniu...@googlegroups.com
Hi Guys, 

Finally sai Kiran nakula's solution worked out. thanks kiran..

assertEquals("The email is blank.\nPlease enter an email!",driver.findElement(By.xpath(XXXXXXXX)).gettext());

cheers guys
Reply all
Reply to author
Forward
0 new messages