robot framework CSS attributes split string - coderslyf/articles--get-started GitHub Wiki
how to find the css style attribute of a particular html element using Robot Framework? up vote 2 down vote favorite
I am writing an automation test script using Robot Framework & Selenium2Library for testing our web application. One of my test cases involves checking the CSS style attribute of an HTML tag.
Is there any option in Robot Framework to obtain the CSS style attributes of an html element?
Here is my HTML:
Now, I have to store the background color of this particular html tag into a variable ${bg_color}. Is there any specific keyword in Robot Framework to do this process?
Can you please suggest an effective way to handle this situation? selenium2 robotframework shareimprove this question
edited Oct 26 '15 at 11:11 bad_deadpool 1,818922
asked Oct 25 '15 at 6:10 Arun Ramachandran 641311
add a comment 3 Answers active oldest votes up vote 2 down vote accepted
You can use the Selenium2Library Get Element Attribute keyword to get the style attribute:
| | ${style}= | Get element attribute | id=check_style@style
You can then either use a regular expression to find the background color attribute or do some additional parsing. The latter would be easier to do in python than with robot keywords.
For example, if you understand regular expressions, something like the following might work. Of course, you'll probably want to add some bullet-proofing.
| |
Note: you might not get the same literal value as is in the raw HTML. For example, on my machine ${color} comes back as rgb(255, 204, 0) even though the color in the HTML is #ffcc00. shareimprove this answer
answered Oct 27 '15 at 14:04 Bryan Oakley 59928
Could you please take a look at this issue here Handling different input fields using RF – Arun Ramachandran Oct 28 '15 at 5:27 add a comment up vote 2 down vote
You will find the answer here:
http://robotframework.org/Selenium2Library/doc/Selenium2Library.html
For reference, in this case I would use the keyword "Get Element Attribute" and compare it against the expected result. shareimprove this answer
answered Oct 27 '15 at 10:55 Cronax 1907
I have also tried Get Element Attribute ! Here is my code : ${bg_color} = Get Element Attribute id=check_style . But while executing this code, am getting an error Attribute locator id=check_style doesn't contain an element locator ! Is it the right way to use this specific keyword ? Can you please provide an example ? How to obtain a particular attribute ( eg. background-color ) using this keyword ? – Arun Ramachandran Oct 27 '15 at 11:04
@ArunRamachandran You may be experiencing a spacing issue, it's hard to know from a comment what the issue is, you should add the things you tried to your question. – Cronax Oct 27 '15 at 11:07
Can you please provide an example by using `Get Element Attribute' ! I have searched alot , But couldn't find any proper example related to this specific keyword. – Arun Ramachandran Oct 27 '15 at 11:15 add a comment up vote 1 down vote
You first need to get the attribute of the element by using 'Get Element Attribute' of selenium2Library:
${style}= | Get element attribute | id=check_style@style
Once you get the attribute like "width:20px;height:20px;background-color:#ffcc00;"
then, you can get background-color very easily from this string by using 'Split String' of String library:
${style} | ${bgcolor} | Split String | ${style} | background-color:
You will get the back ground color in ${bgcolor}:
Log | ${bgcolor}