getAttribute("style") is Returning Blank Value

4,734 views
Skip to first unread message

prem kumar

unread,
Jul 20, 2012, 10:30:46 AM7/20/12
to seleniu...@googlegroups.com
Hi All,
 
I am getting the Blank Value and null value while using below code..

 

Driver.get(http://www/quikr.com/");

(Driver.findElement(By.xpath("//*[@class ='select_city_main']")).getAttribute("style"));(To get style Value) Returning Blank 
(Driver.findElement(By.xpath("//*[@id='header']")).getAttribute("h3"));(To get Tag Value) Returning as "null"

Iam using FF-12 and Webdriver 2.21.0

Please let me know what modification I need to do for this code..

Regards,

Kumar

 

Mark Collin

unread,
Jul 20, 2012, 11:02:35 AM7/20/12
to seleniu...@googlegroups.com

It looks like you are getting confused.

 

H3 is an element tag, not an attribute.  Style is an attribute but unless it has been explicitly set in the HTML markup it will be blank.

 

It looks like you want to get css values, not element attributes.  Try using:

 

driver.findElement(By.id("foo")).getCssValue("background-color");

 

You will of course need to know which css value you are looking for.

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

prem kumar

unread,
Jul 25, 2012, 8:52:34 AM7/25/12
to seleniu...@googlegroups.com
Hi Mark,
 
Thanks for ur clarification.. But how can I get the value of "color:white"  and "Select a classifieds category to find what you are looking for"... Please look into the below HTML code for the values..
div id="main" style="width:780px">
<div id="block" style="width:758px">
<div id="header">
<h3>Select a classifieds category to find what you are looking for</h3>
</div>
<div class="blank5"></div>
 
<div class="new">
<div id="site_header">
<div style="width:249px; float:left; text-align:center">
<a target="_parent" href="http://www.quikr.com">
<div class="blank1"></div>
<a style="color:white" onmousedown="qkr_showcity_popup(this,'green_dhtml',event)">
<div class="select_city_main">Select City</div>
</a>
<div id="green_dhtml">
</div>

Peter Gale

unread,
Jul 25, 2012, 9:07:26 AM7/25/12
to Selenium Users
If that' is the html you were trying to run this command on:

(Driver.findElement(By.xpath("//div[@class ='select_city_main']")).getAttribute("style"));

Then you should not expect to get the response "color:white" as you are pointing to the wrong element.

You'd need to point to the specific <a> tag not an unrelated div tag.

This should return "color:white":

(Driver.findElement(By.xpath("//div[@class ='blank1']"/a")).getAttribute("style"));

This should return "Select a classifieds category to find what you are looking for":

(Driver.findElement(By.xpath("//div[@id='header']"/h3")).getText());

This html looks odd ... embedding a <div> inside an <a> looks a somewhat perverse use of html, for example  ...


Date: Wed, 25 Jul 2012 05:52:34 -0700
From: premia...@gmail.com
To: seleniu...@googlegroups.com
Subject: [selenium-users] Re: getAttribute("style") is Returning Blank Value
--
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/-/Atg_ZcNPRKMJ.

Peter Gale

unread,
Jul 25, 2012, 9:18:53 AM7/25/12
to Selenium Users
Adding to what Mark said previously, it seems that you're also confused between CSS and inline style attributes.

CSS values are cascading, so a CSS setting at one level is inherited by the elements below it, whereas html attribute values are not inherited, so an element does not inherit the style setting of a parent element.


From: peterjef...@hotmail.co.uk
To: seleniu...@googlegroups.com
Subject: RE: [selenium-users] Re: getAttribute("style") is Returning Blank Value
Date: Wed, 25 Jul 2012 14:07:26 +0100

prem kumar

unread,
Jul 27, 2012, 8:21:30 AM7/27/12
to seleniu...@googlegroups.com
Thanks,  Peter For your replay..

But now I am getting the below error for the modified code as per ur instructions...Please do the need full

Code is:
(Driver.findElement(By.xpath("//div[@class ='select_city_main']/a")).getAttribute("style"));

Below is the error I am getting..

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//div[@class ='select_city_main']/a"}
Command duration or timeout: 110 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.21.0', revision: '16552', time: '2012-04-11 19:09:00'
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.7.0_03'
Driver info: driver.version: RemoteWebDriver
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:175)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:128)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:459)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:227)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:312)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:344)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:219)
    at GetAttributeExample.main(GetAttributeExample.java:23)
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Unable to locate element: {"method":"xpath","selector":"//div[@class ='select_city_main']/a"}
Build info: version: '2.21.0', revision: '16552', time: '2012-04-11 19:09:00'
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.7.0_03'
Driver info: driver.version: unknown
    at <anonymous class>.<anonymous method>(file:///C:/DOCUME~1/CB08778/LOCALS~1/Temp/anonymous6846617187997158341webdriver-profile/extensions/fxdr...@googlecode.com/components/driver_component.js:6573)
    at <anonymous class>.<anonymous method>(file:///C:/DOCUME~1/CB08778/LOCALS~1/Temp/anonymous6846617187997158341webdriver-profile/extensions/fxdr...@googlecode.com/components/driver_component.js:6581)
    at <anonymous class>.<anonymous method>(file:///C:/DOCUME~1/CB08778/LOCALS~1/Temp/anonymous6846617187997158341webdriver-profile/extensions/fxdr...@googlecode.com/components/command_processor.js:9774)
    at <anonymous class>.<anonymous method>(file:///C:/DOCUME~1/CB08778/LOCALS~1/Temp/anonymous6846617187997158341webdriver-profile/extensions/fxdr...@googlecode.com/components/command_processor.js:9779)
    at <anonymous class>.<anonymous method>(file:///C:/DOCUME~1/CB08778/LOCALS~1/Temp/anonymous6846617187997158341webdriver-profile/extensions/fxdr...@googlecode.com/components/command_processor.js:9725)



On Wednesday, July 25, 2012 6:37:26 PM UTC+5:30, PeterJef...@Hotmail.co.uk wrote:
If that' is the html you were trying to run this command on:

(Driver.findElement(By.xpath("//div[@class ='select_city_main']")).getAttribute("style"));

Then you should not expect to get the response "color:white" as you are pointing to the wrong element.

You'd need to point to the specific <a> tag not an unrelated div tag.

This should return "color:white":

(Driver.findElement(By.xpath("//div[@class ='blank1']"/a")).getAttribute("style"));

This should return "Select a classifieds category to find what you are looking for":

(Driver.findElement(By.xpath("//div[@id='header']"/h3")).getText());

This html looks odd ... embedding a <div> inside an <a> looks a somewhat perverse use of html, for example  ...


Date: Wed, 25 Jul 2012 05:52:34 -0700
From: premia...@gmail.com
To post to this group, send email to selenium-users@googlegroups.com.
To unsubscribe from this group, send email to selenium-users+unsubscribe@googlegroups.com.

Mark Collin

unread,
Jul 27, 2012, 8:35:51 AM7/27/12
to seleniu...@googlegroups.com

That is because the element doesn’t exists (as the exception told you.

 

I think you need to take a step back and learn how to use XPath and how to understand HTML.

 

If you don’t understand HTML you are going to have a lot of problems testing websites.  Once you have learnt HTML then go and find out about XPaths as you are obviously getting confused at the moment.

 

Based on the html you have supplied so far this will return you something:

 

driver.findElement(By.xpath("//a[div[@class ='select_city_main']]")).getAttribute("style");

 

However I don’t know if it will return what you want (I’m not even sure you know what you want).

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

--

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

Peter Gale

unread,
Jul 27, 2012, 8:41:17 AM7/27/12
to Selenium Users
Prem

What needs to happen is that you need to learn to resolve fairly basic issues like this for yourself.

Firstly, read up on the use of Xpath expressions so that you have confidence to create and edit them for yourself. There's lots of threads on this forum which discuss XPath problems and the type of syntax that you'll encounter when working with web pages.

Secondly, ensure that you have Firebug or Firepath installed in Firefox, Developer Tool in IE (8 onwards) or an equivalent, so you can get easily navigate through the html of a web page and get suggestions as to what XPath expressions might identify individual elements.

When you're confident with XPath, you might want to look as CSS locator expressions also, as CSS locators are sometimes better/easier to use than XPath.

There's not a lot to either Xpath or CSS that you need to know really, at least to get started.

We here are happy to help you get started with Selenium, but we're not a service to do all the hard work for you.

Peter


Date: Fri, 27 Jul 2012 05:21:30 -0700
From: premia...@gmail.com
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] Re: getAttribute("style") is Returning Blank Value
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/-/P_a_jhhAgc4J.

Pankaj Dhapola

unread,
Jul 27, 2012, 9:06:53 AM7/27/12
to seleniu...@googlegroups.com
Hello Prem,
I inspected the site (quickr.com) and found the element that you mentioned in xpath (as class="select_city_main") for which you are looking for the style= "color:white" attribute.

Man, look at screenshot attached,

Now this code
(Driver.findElement(By.xpath("//*[@class ='select_city_main']")).getAttribute("style")) 
above code will return you the DIV element, which doesn't have the style attribute, only class="select_city_main", and the style attribute you are looking for is of Anchor tag which holds this DIV element. So to get that attribute (i.e. style) you need to grab the Anchor element. 
How about using this XPATH = "//*[@id="site_header"]/div[1]/a[2]"

:) and due, for the second question 
h3 is a tag not an attribute of "<DIV id="header">", the H3 tag that you are looking for is child node of this DIV tag that you have fetched using below code
(Driver.findElement(By.xpath("//*[@id='header']")).getAttribute("h3")); 

if you want the text "Select a classifieds category to find what you are looking for"
then use code something like this
(Driver.findElement(By.xpath("//*[@id='header']")).getText();

if this doesn't work then you will have to grab the h3 tag and then use getText() method.

For a better outlook, play with DOM in firebug.
:)



On Friday, July 20, 2012 8:00:46 PM UTC+5:30, prem kumar wrote:
DOMPrint.JPG
Reply all
Reply to author
Forward
0 new messages