how to get all html attributes for a tag

2,041 views
Skip to first unread message

gouse basha

unread,
Aug 13, 2012, 6:30:34 AM8/13/12
to seleniu...@googlegroups.com
I am trying to get all the attribute associate with the element ,but webdriver does have any api to achieve,so iam trying to do with javaScriptexecutor some thing like this 
<input type="hidden" name="_custom__userName"
									id="_custom__userName"
									value='CRD.2.13.6User15AOK_BN_F1_U1_TS1_DS1_NORMAL_SUBSET1' />
i want this value
type="hidden" name="_custom__userName"
									id="_custom__userName"
									value='CRD.2.13.6User15AOK_BN_F1_U1_TS1_DS1_NORMAL_SUBSET1'
so below is the code i am trying ArrayList parentAttributes = (ArrayList) ((JavascriptExecutor)selenium).executeScript( "var s = []; var attrs = arguments[0].attributes; for (var l = 0; l < attrs.length; ++l) { var a = attrs[l]; s.push(a.name + ':' + a.value); } ; return s;", element); but iam getting timeout exception org.openqa.selenium.WebDriverException: null (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 203 milliseconds

--
Thanks & Regards,
Basha
9538029173

Oskar

unread,
Aug 13, 2012, 6:40:24 AM8/13/12
to seleniu...@googlegroups.com
hi

And this:

        WebElement element = driver.findElement(By.xpath("somePath));
        String id = element.getAttribute("id");

doesn't work?. Do you know the name of the attributes your looking for? Or are you just trying to get all?

Greetings

Peter Gale

unread,
Aug 13, 2012, 6:44:33 AM8/13/12
to Selenium Users
As it stands this is a Javascript question, not a Selenium one.

First make sure that your Javascritp is valid by executing it manually in the browsers (not through Selenium).


Date: Mon, 13 Aug 2012 16:00:34 +0530
Subject: [selenium-users] how to get all html attributes for a tag
From: mohamm...@gmail.com
To: seleniu...@googlegroups.com
--
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 https://groups.google.com/groups/opt_out.
 
 

Mark Collin

unread,
Aug 13, 2012, 7:15:30 AM8/13/12
to seleniu...@googlegroups.com

The problem is that some attributes are implicitly set, and some need to be explicitly set.  If you want to know what attributes have been explicitly set you would have to do something like this:

 

//The parent tag

WebElement myElement = driver.findElement(By.tagName("div"));

String contents = (String)((JavascriptExecutor)driver).executeScript("return arguments[0].innerHTML;", myElement);

 

Then you could take contents and pull out the attributes, there are couple ways you could do this:

 

·         Use regex

·         Create an XML document from the String contents and then pull out all the attributes

 

If you want to use regex you would probably want to isolate your tag and pull it out of the string and then you could break it up into an array splitting on whitespace.  If you create an XML document it will depends on which XML libraries you use, I’ve found xom.nu to be pretty lightweight and good.

 

From: seleniu...@googlegroups.com [mailto:seleniu...@googlegroups.com] On Behalf Of gouse basha
Sent: 13 August 2012 11:31
To: seleniu...@googlegroups.com
Subject: [selenium-users] how to get all html attributes for a tag

 

I am trying to get all the attribute associate with the element ,but webdriver does have any api to achieve,so iam trying to do with javaScriptexecutor some thing like this 

--

Reply all
Reply to author
Forward
0 new messages