Method storeAttribute works perfectly when I need to get an attribute like href:
xpath=(//span[#class='active'])#href
But how should I use it to get the whole element (span in this example)? I mean the whole html source text of the element.
You can do this just with the storeText command.
So your target would be just:
xpath=(//span[#class='active'])
Set Command to storeText and set Value to the name of the desired variable.
Related
I need to extract an attribute "sub elemement" after initial selector.
I'm using this syntax as selector:
${first_video}= Get WebElement xpath:(//div[contains(#data-store, 'videoID')])[1]
This correctly give me this single element part:
<div class="alpha" data-store="{"videoID":"1234","playerFormat":"inline","playerOrigin":"video_home","external_log_id":null,"external_log_type":null,"playerSuborigin":"entry_point",
"useOzLive":false,"playOnClick":true,
"videoScrollUseLowThrottleRate":true,"playInFullScreen":false,"type":"video","src":"https:\/\/video.test.com\/v\/a\/123_n.mp","width":320,"height":180,
"videoURL":"https:\/\/www.test.com\/test\/videos\/12345\/","disableLogging":false}" data-sigil="Video">
</div>
My goal is to get the VideoURL attribute.
I've tried with
${first_video_link}= Call Method ${first_video} value_of_css_property data-store.videoURL
but it gaves me empty output.
Is there any specific syntax that I'm missing?
Many Thanks
Get the value of the data-store atrribute:
${val}= Get Element Attribute ${your locator}
It appears to be a proper json, so convert it to a python dictionary and then just access its key:
${as json}= Evaluate json.loads("""${val}""") json
Log To Console The videoURL is ${as json['videoURL']}
Variable attribute not getting passed into other file.
I have my variables for element locators stored in one file and I have assertions done in another file which has worked fine until now and separates things out nicely. I am doing an assertion to check that an element exists and its attribute(value) is not blank. If I write it on one page as follows it works perfectly. This use the selenium library keywords should not be equal and Get Element Attribute just to note.
${EXAMPLE} get element attribute class=test test-data
should not be equal ${EXAMPLE} ${EMPTY}
But If I separate them out into different files. So a locators.robot file:
#Locater File
${EXAMPLE} get element attribute class=test[test-data]
And an Assertions.robot:
#Assertion File
should not be equal ${EXAMPLE} ${EMPTY}
It stops working. If I use a selenium library assertion like page should contain element then it works, so I know I am pulling in the other Resource correctly. I have a feeling I may need to store the attribute in another variable somehow and actually assert against that. Any ideas would be great. Thanks in advance.
Suppose you have html code like this as you given in other question -
<div id="top-list">
<div data-version="12345" data-list="1" data-point="10">
Way 1 - Less recommended -
This is how my assertion.robot looks like -
*Settings
Library SeleniumLibrary
Resource Locator.robot
*Test Cases
Test attributes Locator
Open Browser file:///C:Desktop/testxpath.html chrome
${attribute_value}= Get Element attribute ${Datalist_locator_with_all_attribt} data-list
should not be equal ${attribute_value} ${EMPTY}
The locators are in locator.robot file. I'm not calling Get Element Attribute keyword in the locators because doing so there will be no link of directly executing it and referencing back the return value of it in testcase... So just keeping the locators in the locator file nothing else. This locators are accessible when I did Resource Locator.robot in my assertion.robot file. As you can see the Get Element Attribute element take first argument locator of element and second argument is nothing but the attribute name of which value you need. And this keyword returns value of attribute that supplied as second argument. -
*Variables
${Datalist_locator_with_all_attribt} xpath://div[#data-version='12345' and #data-list='1' and #data-point='10']
${locator_with_single_attribute} xpath://div[#data-version='12345']
Output
Way 2 - More Recommended -
Wrap the Get Element Attribute and Should Not Be Equal keywords in one single keyword. and dump it in another keyword file or create *keywords section in locator.robot file itself. Doing this your assertion.robot file will look like this -
*Test Cases
Test attributes Locator
Open Browser file:///C:/Desktop/testxpath.html chrome
Attribute values should not be empty
and locator.robot will look like this. You can make it more generic though -
*** Variables
${Datalist_locator_with_all_attribt} xpath://div[#data-version='12345' and #data-list='1' and #data-point='10']
${locator_with_single_attribute} xpath://div[#data-version='12345']
*** Keywords
Attribute values should not be empty
${attribute_value}= Get Element attribute ${Datalist_locator_with_all_attribt} data-list
should not be equal ${attribute_value} ${EMPTY}
Output
I was testing my website using RF. The problem is, every time the modal is opened, a different id(locator) will be set on the textbox that I want to input my text. How do you get value of this locator?
I was supposed to try Get Element Attribute but then it cannot support my problem since it still requires a specific locator.
In ROBOT Framework (RF), the locator can be accessed by several ways. Please refer and read this link: http://robotframework.org/Selenium2Library/Selenium2Library.html
The most common way to access the locator is by id such as :
Input Text id:username # Element with id 'username'.
Input Text id:password # Element with id 'password'. you can also use 'Input Password' keyword.
However, if the 'id' element is so dynamic which it keep changing, then the best alternative is to use either ABSOLUTE XPATH expression or CSS selectors. Install the XPATH add-on in your web browser. For firefox, just install ChroPath.
Then, get the ABSOLUTE Xpath element of that username & password text box. Let's assume we know the absolute xpath expression already, so in ROBOT, you can write like below.
${login_absolute_xpath}= Set Variable xpath=/html[1]//div[7]/form[1]/div[1]/input[1]
${password_absolute_xpath}= Set Variable xpath=/html[1]//div[7]/form[1]/div[2]/input[1]
Wait Until Page Contains Element xpath=${login_absolute_xpath}
Input Text xpath=${login_absolute_xpath}
Input Text xpath=${password_absolute_xpath}
...
This should works. Please let me know if this helps.
I am using selenium for automating test cases for web application in that I have to get tool tip text
I tried
String result =element.getAttribute("span");
but this is retuning null. how can I get the text ?
element.getAttribute("span");
span must not be an attribute of the given element. I guess you've misunderstood, the getAttribute() method.
For e.g.
Example
For the above anchor tag, in order to get the title attributes' value, you can use :
element.getAttribute("title");
where element refers to the above anchor element.
I am new to selenium.
I tried to save a text from the xpath using storeText and the target is //*[#id='mathq2'].
the base URL is http://timesofindia.indiatimes.com/.
I am getting this error
[info] Executing: |storeText | //*[#id='mathq2'] | wwww |
[error] Invalid xpath [2]: //*[#id='mathq2']
Hi Danny got the same error in timesof india website. This is what i did.
I recorded the element (by entering something in the text box next to it) then clicked on the value like 8+0 = and then selected the radio button above it.
So my selenium showed
click id=mathq2
from there i chose the xpath in the Target dropdown and the xPath seemed to show
//span[#id='mathq2']
so finally I replaced my step for storeText as
storeText //span[#id='mathq2'] addition
and then it stored that value 8+0 = in the variable 'addition'.
You are getting this error, because there is no element with such xPath at the main page of http://timesofindia.indiatimes.com/.
Why do you think it is there?
You can use xPath Checker (Firefox plugin) to check if the xPath is proper and the element exists.
yes, ID is there, but text is changing dynamically.
You need to add the xpath= prefix in your target, and it seems that the xpath is also prepended with a period:
|storeText | xpath=.//*[#id='mathq2'] | wwww |
You would also need to ensure that your script waits for the initial advertisement page to unload.