This Question is Answered

1 "correct" answer available (4 pts) 2 "helpful" answers available (2 pts)
5 Replies Last post: Jan 8, 2009 6:17 AM by Chetan  
Chetan Beginner 17 posts since
Oct 21, 2008
Currently Being Moderated

Dec 15, 2008 6:17 AM

How to get name of the node from xpath or selenium

Hi,

 

I am new to selenium and we are implementing SeleniumRC with Java in our project. I spent full day to find - how to get node/tag name of the element. Please provide some info.

 

Following is the example :

 

//table[@class='DialogForm']//*[contains(@id,'subProject')] according to this xapth I am able to identify element but I have to take action according to type of element, like if it is dropdown menu I ll select option, if it is text input I have to type or click if its radio button.

 

I tried following but this gives me textof the element:

//table[@class='DialogForm']//*[contains(@id,'subProject')]//*[name()]

 

Regards,

Chetan

gim_h Almighty 736 posts since
Oct 17, 2007
Currently Being Moderated
1. Dec 15, 2008 7:05 AM in response to: Chetan
Re: How to get name of the node from xpath or selenium

if you want to know whether the input element is a textbox or a radio, you should get the element's 'type' attribute value not the element's tag name or node name, for example:

    public void testInputType() throws Exception {
        selenium.open("http://www.google.com/");
        //assert the search button's type is submit
        assertEquals("submit", selenium.getAttribute("btnG@type"));
    }
Todor Tukov Pro 128 posts since
Oct 17, 2007
Currently Being Moderated
3. Jan 7, 2009 5:51 AM in response to: Chetan
Re: How to get name of the node from xpath or selenium

Hi,

 

I'm not aware for the Java syntax but in C# it would be

 

String nodeName = selenium.GetEval("this.page().findElement('<locator>').nodeName")

 

Where <locator> is your xpath or id or whatever.

After this statement nodeName should contain the name of the html tag, e.g. INPUT, SELECT, DIV, etc.

 

I hope this helps and you can modify it for Java and use it

gim_h Almighty 736 posts since
Oct 17, 2007
Currently Being Moderated
4. Jan 7, 2009 6:47 AM in response to: Chetan
Re: How to get name of the node from xpath or selenium

select element also has type attribute, its type is either 'select-one' or 'select-multiple'. but getAttribute command doesn't correctly obtain it. the reture value is always "null"

if your webapp doesn't have any other kinds of form elements whose type value returned by getAttribute is "null",  i think you can simply consider "null" as type of select.

More Like This

  • Retrieving data ...