Selenium Forum: Functional And Regression Testing Tool.
Would you like to react to this message? Create an account in a few clicks or log in to continue.
Search
Display results as :
Advanced Search
Latest topics
AEM Training | Free Online DemoWed Apr 21, 2021 5:45 pmazharuddin
c# PageFactory - issue initializing elementsFri Nov 01, 2019 8:40 pmthegoatboy
Selenium making automatic connection to random urlsMon Jul 08, 2019 12:58 pmrepairtechsolutions1
How can we design the custom framework in Selenium RCMon Jun 24, 2019 2:26 pmrandybonnettes
What are the new features in Selenium 3.0Tue Jun 18, 2019 5:37 pmpappyvicky
What are you using Selenium for? Fri Apr 12, 2019 3:52 amzhl
LIMITATIONS OF SELENIUMWed Apr 10, 2019 11:23 amswara
Navigation
 Portal
 Index
 Memberlist
 Profile
 FAQ
 Search

Go down
avatar
mgharikrishnan
Active particpant
Active particpant
Posts : 10
Join date : 2011-06-20

How to get id or xpath of a element based on the text present in it Empty How to get id or xpath of a element based on the text present in it

Wed Jul 27, 2011 4:35 pm
Hi friends,

Please help me with this. I have a text say "Gary" in a web page, which is displaying in a table. I need to get the xpath or the id of the location, where word "Gary" is present in the web page. IS it possible to achieve it. If yes, please help me in doing it.

Thanks,
Hari
avatar
freesky
Amateur
Amateur
Posts : 72
Join date : 2011-04-13

How to get id or xpath of a element based on the text present in it Empty Re: How to get id or xpath of a element based on the text present in it

Thu Jul 28, 2011 10:05 am
use 'Xpath checker'
avatar
mgharikrishnan
Active particpant
Active particpant
Posts : 10
Join date : 2011-06-20

How to get id or xpath of a element based on the text present in it Empty Re: How to get id or xpath of a element based on the text present in it

Thu Jul 28, 2011 2:16 pm
Thanks for the reply, freesky. But the thing is that, I want to do it while is execute the script. My problem is that, when I register a user, his username & email id will display in a table in the web page. In the script, i have to search for that username and take the xpath of that username....

~ Harri...
avatar
freesky
Amateur
Amateur
Posts : 72
Join date : 2011-04-13

How to get id or xpath of a element based on the text present in it Empty Re: How to get id or xpath of a element based on the text present in it

Thu Jul 28, 2011 3:04 pm
you want to get the xpath automation according to the text present.
I haven't heard that we can do that yet.
avatar
Arthur
Active particpant
Active particpant
Posts : 23
Join date : 2011-05-23
Location : Pune

How to get id or xpath of a element based on the text present in it Empty Re: How to get id or xpath of a element based on the text present in it

Mon Aug 01, 2011 1:27 pm
Hi,

You can do this using Selenium-RC Just get the xpath of first Element in the table. Now get the same for the second element in the table verify the the change the change should be the row number of table.
With above steps only search the row changing.

This row number can be placed as variable and get the text of each table row in the specific column where it should be seen. When while traversing the element is found then break the loop and the the xpath can be retrieved dynamically.

You cant do this in Selenium IDE this the situation where you have to use the selenium RC.

Thanks.
avatar
mgharikrishnan
Active particpant
Active particpant
Posts : 10
Join date : 2011-06-20

How to get id or xpath of a element based on the text present in it Empty Re: How to get id or xpath of a element based on the text present in it

Mon Aug 01, 2011 9:17 pm
Hi Arthur,

Thanks for your comment. I was able to achieve this task today. I am putting the code here, so that it will be helpful for someone like me.

Code:

//This gets the number of data displaying in the table
int rowCount = (Integer)selenium.getXpathCount("//table[@id='pending-reg']/tbody/tr");
//Looping thru the data
for(int i=3; i<=rowCount; i++)
{
String username = selenium.getText("//table[@id='pending-reg']/tbody/tr["+i+"]/td[1]");
Thread.sleep(500);
System.out.println("Username = "+username);
//Looking for the username Gary
if(finame.equalsIgnoreCase("gary"))
{
//Getting the password of Gary
String password = selenium.getText("//table[@id='pending-reg']/tbody/tr["+i+"]/td[2]");
Thread.sleep(500);
System.out.println("Password = "+password);
}
}
Sponsored content

How to get id or xpath of a element based on the text present in it Empty Re: How to get id or xpath of a element based on the text present in it

Back to top
Permissions in this forum:
You cannot reply to topics in this forum