Find Element by Attribute

11,764 views
Skip to first unread message

dont waste your life

unread,
Feb 28, 2011, 10:36:49 PM2/28/11
to Selenium Users
Hello all,

I'm examining the possibility of moving my automated test scripts to
run off of Selenium. I was doing some digging, but might get a
quicker answer here:

Is it possible to have selenium find an element by any attribute
without xpath?

Such as.. lets say I have an input field:

<input ref="address1" id=6_203050302034_5" />

Is there any way to have it find the input field by specifically
telling selenium to look for the attribute "ref" where it matches
"address1".. and for multiple matches to be able to index the matches?

Thanks!

David T

unread,
Feb 28, 2011, 11:52:37 PM2/28/11
to Selenium Users
Don't waste your life,

Selenium can find an element by numerous different "locators"

This is implemented in different ways depending if you are using the
IDE: http://seleniumhq.org/docs/02_selenium_ide.html#locating-elements
Or a Core API: http://release.seleniumhq.org/selenium-core/1.0/reference.html#locators

I realize documentation doesn't seem the easiest to find but it is
there in one form or another.

David Troyer

On Feb 28, 10:36 pm, dont waste your life <nathand...@gmail.com>
wrote:

yuuzhantar

unread,
Mar 1, 2011, 3:52:30 AM3/1/11
to Selenium Users
webDriver.findElement(By.xpath("//input[@ref='address1']"))

Jayakumar C

unread,
Mar 1, 2011, 10:08:34 AM3/1/11
to seleniu...@googlegroups.com
@Don't waste your life,
 
Is there any specific reason for not using xpath ?,
 
I hope, this will do the trick for your case.Here you can increment the index to get the successive matching elements. 
xpath=(//*[@ref='address1'])[0]
 


 

--
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.




--
Jayakumar

Mark Collin

unread,
Mar 1, 2011, 10:17:28 AM3/1/11
to seleniu...@googlegroups.com

It won’t.

 

When you use the [1] notation you are specifically looking for something that is the first child of the parent element.  You may have 10 1st childs, but you will only interact with the first one because selenium will find the first match and run with it.

 

If you use [0] you will never find anything because there are no 0 childs.


-- This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited. If you have received this email in error please notify postm...@ardescosolutions.com

gBurgur

unread,
Mar 1, 2011, 10:20:26 AM3/1/11
to seleniu...@googlegroups.com

And if you are not comfortable with xpath checkout “XPath Checker”.  It will supply you with an xpath expression.

 

From: seleniu...@googlegroups.com [mailto:seleniu...@googlegroups.com] On Behalf Of Jayakumar C
Sent: Tuesday, March 01, 2011 10:09 AM
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] Find Element by Attribute

 

@Don't waste your life,

Jayakumar C

unread,
Mar 1, 2011, 11:37:28 AM3/1/11
to seleniu...@googlegroups.com
@Mark Collin,
Thats my bad , the index should start from 1.
But your explanation is valid,if the xpath  is   //*[@ref='address1'][1]
and not xpath=(//*[@ref='address1'])[1]
 
Pls check these links,

Ambika Rajagopalan

unread,
Mar 1, 2011, 3:30:32 PM3/1/11
to seleniu...@googlegroups.com
I am facing Element Not found error in IDE. I am trying to select an item from the dropdown using the Select command after the login page. I am not sure how to fix this. Need help

I see that the element not found error comes before the page is completely loaded, which means I tried to use Selectandwait command as well still the test case fails

gBurgur

unread,
Mar 2, 2011, 1:53:41 PM3/2/11
to seleniu...@googlegroups.com

I the dropdown a special type?  i.e.  jQuery

DarthOpto

unread,
Sep 13, 2012, 1:36:11 PM9/13/12
to seleniu...@googlegroups.com
If you have the id why not just use driver.findElement(By.id(6_20305032034_5) ?

jeevan

unread,
Sep 14, 2012, 12:01:43 AM9/14/12
to seleniu...@googlegroups.com
sure.....you can use like :

driver.findElement(By.cssSelector("input[ref=address1]"))

Webdriver can get the attribute of the tag also:

driver.findElement(By.id("test")).getAttribute("ref")

Thanks,
Jeevan


--
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/-/eroXJJHT3N0J.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages