how to verify text present in a page

1,411 views
Skip to first unread message

pradeep

unread,
Aug 31, 2013, 8:24:53 AM8/31/13
to seleniu...@googlegroups.com
Hi,

Using Selenium WebDriver, i'm trying to automate http://newtours.demoaut.com portal.

In this portal, once we register, the below confirmation message is displayed:

 Dear ,

Thank you for registering. You may now sign-in using the user name and password you've just entered.

Note: Your user name is xxx.


How to verify the above text in the WebDriver code? i tried by taking xpath as below, but after that i'm not sure how to  validate it.


 WebElement text =driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[3]/td/p/font/b"));

Any help would be appreciated.


Thanks,

Pradeep

Dhiraj

unread,
Sep 2, 2013, 1:59:00 AM9/2/13
to seleniu...@googlegroups.com
Hello Pradeep,

Use the following code (If location of text on page not matters)::

driver.getPageSource().contains(" ");

Thanks,
Dhiraj | Skype: dhiraj.chaubey

Oscar Rieken

unread,
Sep 2, 2013, 4:00:04 PM9/2/13
to seleniu...@googlegroups.com
On Mon, Sep 2, 2013 at 1:59 AM, Dhiraj <dhira...@gmail.com> wrote:
Hello Pradeep,

Use the following code (If location of text on page not matters)::


Don't do this. there is no reason to search the whole page for the text you want. It would be better to make sure its in the right place. 
driver.getPageSource().contains(" ");
 

Thanks,
Dhiraj | Skype: dhiraj.chaubey



On Saturday, 31 August 2013 17:54:53 UTC+5:30, pradeep wrote:
Hi,

Using Selenium WebDriver, i'm trying to automate http://newtours.demoaut.com portal.

In this portal, once we register, the below confirmation message is displayed:
 Dear ,
 
 
Thank you for registering. You may now sign-in using the user name and password you've just entered.

Note: Your user name is xxx.


How to verify the above text in the WebDriver code? i tried by taking xpath as below, but after that i'm not sure how to  validate it.



 WebElement text =driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[3]/td/p/font/b"));

doing something like this is very brittle its depending that the object you want is in this exact location. better to try and locate the exact item you want and then get its text. I would suggest looking at the the http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebElement.html 
to see how to get the text of an element. 
and reply with the html of the part of the page that contains your text 
 
Any help would be appreciated.


Thanks,

Pradeep

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, 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/7649eeb3-1ca6-4d96-8961-90fdcf5e46c9%40googlegroups.com.

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

Bhupesh Pant

unread,
Sep 3, 2013, 2:08:45 PM9/3/13
to seleniu...@googlegroups.com
use Assert.assertTrue(text.getText().contains("Thank you for registering. You may now sign-in using the user name and password you've just entered."));

Ashish Juyal

unread,
Sep 4, 2013, 5:04:42 AM9/4/13
to seleniu...@googlegroups.com
Hi Bhupesh 
                   customize your xpath rather than giving absolute xpath try to give relative xpath and give .getText() and assert it .


Regards
Ashish Juyal

Christian LeMoussel

unread,
Sep 5, 2013, 2:41:32 AM9/5/13
to seleniu...@googlegroups.com
Hi Bhupesh,

as indicated by Ashish, you must customize your xpath.

For ex.
string sText = webdriver.findElements(By.XPath("//*[text() = '" + sTextToCheck + "']")).getText();
System.out.println(text);

Regards
Reply all
Reply to author
Forward
0 new messages