Reading text from Xpath location and store as a string

2,740 views
Skip to first unread message

keshav

unread,
Jan 5, 2011, 11:27:43 AM1/5/11
to Selenium Users
Hi All,

I want to read the text from the location using Xpath and store the
string in a variable and print it.

Can anyone help me????

i used gettext() method but it throws the error

String a1 = ("//h3/a");
String b= gettext("//h3/a");
System.out.println(b);

Thanks,
Keshav

BillR

unread,
Jan 5, 2011, 7:17:56 PM1/5/11
to Selenium Users
Is this java? If so, you have to create a selenium client and use it,
e.g.

selenium = new DefaultSelenium("localhost", 4444, "*firefox",
"http"//somewhere.com");
selenium.start();
selenium.open("/");
String p = selenium.getText("//div[@id='data_provider_area']/h3["+i
+"]/a");

keshav

unread,
Jan 6, 2011, 1:21:47 AM1/6/11
to Selenium Users
Hey i got the solution by making use of gettext.

String b= selenium.getText("//h3/a"); and its working fine.

If there is anyother method plz reply

Thanks,
Keshav

sriharsha a

unread,
Jan 6, 2011, 5:04:00 AM1/6/11
to seleniu...@googlegroups.com
If the text ur getting is displayed to the user , you can use assertEquals() to make sure getting the correct one.

Thanks,
Sriharsha


Thanks,
Keshav

--
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.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.


sai krishna

unread,
Jan 6, 2011, 6:37:13 AM1/6/11
to seleniu...@googlegroups.com
I didnt get u sriharsha ..

See v have some values in  excel entered manually ..  can we compare the values from the excel with the values on the webpage .. ??

is this possible ??

or write the values n compare values within the excel ..

sriharsha a

unread,
Jan 6, 2011, 6:54:49 AM1/6/11
to seleniu...@googlegroups.com
This is how you should be doing assertions ....
you are using getText() to get the text of an element which has text, and storing it in string variable.
Are the values read from excel sheet and values in stored in string variable from getText() same , if yes
use assertEquals() for assertion.

hope this helps .. if not please let me know

Sriharsha

sam

unread,
Jan 6, 2011, 7:03:25 AM1/6/11
to Selenium Users

I got what ur trying to point sriharsha ...

But what im thinking is ..... if v give asserequals with the xpath ..
then i dnt have the necessity to write to excel .. i can directly....
verify in the site..

Lets see this in this way .. say like ..

lets not consider xpath .. lets take compare only the text presents..

for ex: i have abc in the excel .. n i need to check if the same abc
is present in the webpage ..


On Jan 6, 4:54 pm, sriharsha a <harsha.avan...@gmail.com> wrote:
> This is how you should be doing assertions ....
> you are using getText() to get the text of an element which has text, and
> storing it in string variable.
> Are the values read from excel sheet and values in stored in string variable
> from getText() same , if yes
> use assertEquals() for assertion.
>
> hope this helps .. if not please let me know
>
> Sriharsha
>
> On Thu, Jan 6, 2011 at 5:07 PM, sai krishna <hotdude...@gmail.com> wrote:
> > I didnt get u sriharsha ..
>
> > See v have some values in  excel entered manually ..  can we compare the
> > values from the excel with the values on the webpage .. ??
>
> > is this possible ??
>
> > or write the values n compare values within the excel ..
>
> > On Thu, Jan 6, 2011 at 3:34 PM, sriharsha a <harsha.avan...@gmail.com>wrote:
>
> >> If the text ur getting is displayed to the user , you can use
> >> assertEquals() to make sure getting the correct one.
>
> >> Thanks,
> >> Sriharsha
>
> >> On Thu, Jan 6, 2011 at 11:51 AM, keshav <keshav...@gmail.com> wrote:
>
> >>> Hey i got the solution by making use of gettext.
>
> >>>  String b=  selenium.getText("//h3/a"); and its working fine.
>
> >>> If there is anyother method plz reply
>
> >>> Thanks,
> >>> Keshav
>
> >>> --
> >>> 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<selenium-users%2Bunsu...@googlegroups.com>
> >>> .
> >>> For more options, visit this group at
> >>>http://groups.google.com/group/selenium-users?hl=en.
>
> >>  --
> >> 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<selenium-users%2Bunsu...@googlegroups.com>
> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/group/selenium-users?hl=en.
>
> >  --
> > 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<selenium-users%2Bunsu...@googlegroups.com>
> > .

sriharsha a

unread,
Jan 6, 2011, 7:51:29 AM1/6/11
to seleniu...@googlegroups.com
OK !

1) If you want to check what ever value is there in excel , the same is present in the webpage
           selenium.isTextPresent(readDatafromExcel);
readDataFromExcel will return String value which is what is required in isTextPresent()
isTextPresent() returns boolean value .

if (booleanValue){
System.out.println("Strings are equal ")
} else {
   System.out.println("Strings are not equal ")
}

Thanks,
Sriharsha

To unsubscribe from this group, send email to selenium-user...@googlegroups.com.

keshav kumar

unread,
Jan 6, 2011, 8:07:56 AM1/6/11
to seleniu...@googlegroups.com
Can u please let me know how to read the data from particular cell in excel using 
selenium.isTextPresent(readDatafromExcel);

Thanks,
Keshav

If you don't get everything you want, think of the things you don't get that you don't want.

unread,
Jan 6, 2011, 6:54:39 AM1/6/11
to Selenium Users
Hi Keshav
Cross check your Xpath. There is some error related to Xpath. Else
this function was working ok for my case. Or use this function in
SeleneseTestCase class,

BillR

unread,
Jan 6, 2011, 12:59:27 PM1/6/11
to Selenium Users
To read data from Excel, you must use a package for this purpose. E.g.
see code and Excel attached to this thread:

http://groups.google.com/group/selenium-users/browse_thread/thread/deadfffe9ec821e8

lakhwinder sharma

unread,
Jan 6, 2011, 11:33:28 PM1/6/11
to Selenium Users

Hi Keshav,

First u have to give the excel file path. and create a object like
below example

FileInputStream fi = new FileInputStream("D:\\Jobsite\\Country.xls");
Workbook w = Workbook.getWorkbook(fi);
Sheet s = w.getSheet(0);

To find text is present or not.
getcell(0,1)is ur cell location.

selenium.isTextPresent(s.getCell(0, 1).getContents());

Thanks
Lakhwinder Sharma

santhosh soma

unread,
Jan 7, 2011, 12:13:04 AM1/7/11
to seleniu...@googlegroups.com
awesome explanation harsha.......

Thanks
Santhosh.

sam

unread,
Jan 7, 2011, 12:34:39 AM1/7/11
to Selenium Users
Hi folks ..

How do v print the checkbox values .. im able to count the number of
checkbox's n check them too ..

I need to print the value of the checkbox .. or write the values to
the excel ..



This is how the code looks like ..


int checkboxcount=selenium.getXpathCount("//
input[@type='checkbox']").intValue();
System.out.println("The Number of checkbox :"
+checkboxcount );



for (int i = 1; i <= checkboxcount; i++) {

if (selenium.isElementPresent("//
input[@type='checkbox']"))
;
{
// selenium.click("//div[2]/div[2]/div/div/
div[2]/div[2]/div[1]");
// Thread.sleep(2000);
// tempflag=true;
String str= selenium.getText("xpath=(//
input[@type='checkbox'])[position()="+ i + "]");
System.out.println(str);
selenium.click("xpath=(//
input[@type='checkbox'])[position()="+ i + "]");


/* WritableWorkbook workbook =
Workbook.createWorkbook(new File("D:/output.xls"));
WritableSheet sheet = workbook.createSheet("First
Sheet", 0);

Label string = new Label(3, 2, str);
sheet.addCell((WritableCell) string);
workbook.write();
workbook.close();*/

}


On Jan 7, 9:33 am, lakhwinder sharma <lakhwindersharm...@gmail.com>
wrote:

santhosh soma

unread,
Jan 7, 2011, 1:08:20 AM1/7/11
to seleniu...@googlegroups.com
Hi,

Can you please use the following statment to retrive all the values of the check box.

selenium.getAttribute((input[@type='checkbox'])@Value);

Thanks
Santhosh.

sam

unread,
Jan 7, 2011, 1:38:16 AM1/7/11
to Selenium Users
Hi Santhosh ..

String str= selenium.getText("xpath=(//label[@class='x-form-cb-
label'])[position()="+ i + "]");

i was able to get the values .. but not able to write into Excel ..
only the last value is getting written ..

can u tell me were im going wrong ...





On Jan 7, 11:08 am, santhosh soma <santhoshvams...@gmail.com> wrote:
> Hi,
>
> Can you please use the following statment to retrive all the values of the
> check box.
>
> selenium.getAttribute((input[@type='checkbox'])@Value);
>
> Thanks
> Santhosh.
>
> > selenium-user...@googlegroups.com<selenium-users%2Bunsu...@googlegroups.com>
> > .

santhosh soma

unread,
Jan 7, 2011, 2:03:32 AM1/7/11
to seleniu...@googlegroups.com
Can  you try the following logic code

String str=str+","+selenium.getText("xpath=(//
label[@class='x-form-cb-
label'])[position()="+ i + "]");

At the end you will get all the values separated by the cama.

String a[]=str.split(,);

now u will get all the values in array a[]a.

Thanks
Santhosh.


To unsubscribe from this group, send email to selenium-user...@googlegroups.com.

anil kumar

unread,
Jan 7, 2011, 2:05:40 AM1/7/11
to seleniu...@googlegroups.com
Thank you for ur kind reply..
Thanks&Regards
Anil.G

sam

unread,
Jan 12, 2011, 2:00:25 AM1/12/11
to Selenium Users
Hi santhosh ..

THe isssue is that .. i have N number of checkbox for a particular
select option from a drop down ..

Im able to click check the box ..
String str= selenium.getText("xpath=(//label[@class='x-form-cb-
label'])[position()="+ i + "]");

Here "i" value is the no of checkbox...
it keep's incrementing the value according to the checkbox's

so my issue is how do i write all the values of the checkbox into the
same excel ...

right now .. First value is written in a excel and for the value value
of "i" new excel is created and the second value is written .





On Jan 7, 12:05 pm, anil kumar <ggellankianilku...@gmail.com> wrote:
> *Thank you for ur kind reply..
> *
> Thanks&Regards
> Anil.G
>
> On Fri, Jan 7, 2011 at 12:33 PM, santhosh soma <santhoshvams...@gmail.com>wrote:
>
> > Can  you try the following logic code
>
> > String str=str+","+selenium.getText("xpath=(//
>
> >> label[@class='x-form-cb-
> >> label'])[position()="+ i + "]");
>
> > At the end you will get all the values separated by the cama.
>
> > String a[]=str.split(,);
>
> > now u will get all the values in array a[]a.
>
> > Thanks
> > Santhosh.
>
> >> <selenium-users%2Bunsu...@googlegroups.com<selenium-users%252Buns...@googlegroups.com>
Reply all
Reply to author
Forward
0 new messages