get element attribute not working

49 views
Skip to first unread message

Christian Rudolf Tan

unread,
Nov 8, 2018, 3:49:25 AM11/8/18
to robotframework-users
Hi  robot users.

I have some error getting the element attribute. i want to get the class attribute because i am trying to create a test for date picker

User create leave
loadpage.Load salt test
LoginLogout.User login to salt ${AdminUser} ${ADMINRPWD} ${USERSTAFFNAME}
User navigates to leave request
Sleep 3
Keywords.User click element ${LeaveRequestBtn}
User select leave type ${LeaveRequestLeaveType} ${LeavetypeDoc}
sleep 3
User select Staff for leave request ${LeaveReuqestStaff} ${LeaveStaffFname}
User click element ${LeaveRequestStartDate}
check that element exist xpath=//div[@id='ContentPlaceHolder1_ContentPlaceHolder1_calExtStartDate_popupDiv']
${classsttrib} = get element attribute xpath=//tbody[@id='ContentPlaceHolder1_ContentPlaceHolder1_calExtStartDate_daysBody']/tr[2]/[td][1] class
log ${classsttrib}

datepicker.jpg


KEYWORD ${classsttrib} = SeleniumLibrary . Get Element Attribute xpath=//tbody[@id='ContentPlaceHolder1_ContentPlaceHolder1_calExtStartDate_daysBody']/tr[2]/[td][1], class
Documentation:

Returns value of attribute from element locator.

Start / End / Elapsed:20181108 16:31:01.496 / 20181108 16:31:02.972 / 00:00:01.476
00:00:01.391KEYWORD SeleniumLibrary . Capture Page Screenshot
16:31:01.496TRACEArguments: [ "xpath=//tbody[@id='ContentPlaceHolder1_ContentPlaceHolder1_calExtStartDate_daysBody']/tr[2]/[td][1]" | 'class' ]
16:31:01.496DEBUGPOST http://127.0.0.1:52354/session/eba83b94e4377947b449eee1f071482b/elements {"using": "xpath", "sessionId": "eba83b94e4377947b449eee1f071482b", "value": "//tbody[@id='ContentPlaceHolder1_ContentPlaceHolder1_calExtStartDate_daysBody']/tr[2]/[td][1]"}
16:31:01.565DEBUGhttp://127.0.0.1:52354 "POST /session/eba83b94e4377947b449eee1f071482b/elements HTTP/1.1" 200 641
16:31:01.565DEBUGFinished Request
16:31:02.972FAILInvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //tbody[@id='ContentPlaceHolder1_ContentPlaceHolder1_calExtStartDate_daysBody']/tr[2]/[td][1] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//tbody[@id='ContentPlaceHolder1_ContentPlaceHolder1_calExtStartDate_daysBody']/tr[2]/[td][1]' is not a valid XPath expression. (Session info: chrome=70.0.3538.77) (Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.17134 x86_64)
16:31:02.972DEBUGTraceback (most recent call last): File "C:\Python27\lib\site-packages\SeleniumLibrary\__init__.py", line 360, in run_keyword return DynamicCore.run_keyword(self, name, args, kwargs) File "C:\Python27\lib\site-packages\SeleniumLibrary\base\robotlibcore.py", line 102, in run_keyword return self.keywords[name](*args, **kwargs) File "C:\Python27\lib\site-packages\SeleniumLibrary\keywords\element.py", line 384, in get_element_attribute return self.find_element(locator).get_attribute(attribute) File "C:\Python27\lib\site-packages\SeleniumLibrary\base\context.py", line 62, in find_element return self.element_finder.find(locator, tag, True, required, parent) File "C:\Python27\lib\site-packages\SeleniumLibrary\locators\elementfinder.py", line 73, in find parent=parent or self.driver) File "C:\Python27\lib\site-packages\SeleniumLibrary\locators\elementfinder.py", line 125, in _find_by_xpath return self._filter_elements(parent.find_elements_by_xpath(criteria), File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 409, in find_elements_by_xpath return self.find_elements(by=By.XPATH, value=xpath) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 995, in find_elements 'value': value})['value'] or [] File "C:\Python27\lib\site-packages\SeleniumLibrary\keywords\browsermanagement.py", line 437, in execute result = self._base_execute(driver_command, params) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute self.error_handler.check_response(response) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) I hope someone could help me. Best regards, chris

Anne kootstra

unread,
Nov 9, 2018, 3:39:54 AM11/9/18
to robotframework-users
It seems to me that this is the actual issue: 

SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//tbody[@id='ContentPlaceHolder1_ContentPlaceHolder1_calExtStartDate_daysBody']/tr[2]/[td][1]' is not a valid XPath expression.

Have you validated your xpath expression? 

Kind regards,

Anne

Op donderdag 8 november 2018 09:49:25 UTC+1 schreef Christian Rudolf Tan:

Christian Rudolf Tan

unread,
Nov 9, 2018, 6:34:01 AM11/9/18
to robotframework-users
Hi Anne yes I have evaluate my expression. it works when i check in the google built in developers tool. it was actually able to locate because it has id tags. but i was able to correct this issue. i need to go higher to its parent:

<parent element>
  -<table>
       <table>   <---- i was here, it corrected it when i pointed at the parent element. usually it words for me by just the id. for some reason it didnt.
          <element>
               
Thank you very much. I appreciate the help

Pekka Klärck

unread,
Nov 9, 2018, 6:41:04 AM11/9/18
to chant...@gmail.com, robotframework-users
I don't think `[td][1]` you had in the xpath is valid.

Sent from my mobile.

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.
To post to this group, send email to robotframe...@googlegroups.com.
Visit this group at https://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.

Christian Rudolf Tan

unread,
Nov 9, 2018, 7:22:26 AM11/9/18
to robotframe...@googlegroups.com
how do you iterate through the elements in the table. when i check my xpath in google chrome it was able to locate the element.
example: 

//div[@class='mainContainer']/table/tbody/tr/td[2]/div/div/div/div[@id='ContentPlaceHolder1_ContentPlaceHolder1_pnlPopup']/div[@class='popUpContent']/table/tbody/tr[5]/td[3]/div[@id='ContentPlaceHolder1_ContentPlaceHolder1_calExtStartDate_container']/div[@id='ContentPlaceHolder1_ContentPlaceHolder1_calExtStartDate_popupDiv']/div[@id='ContentPlaceHolder1_ContentPlaceHolder1_calExtStartDate_body']/div[@id='ContentPlaceHolder1_ContentPlaceHolder1_calExtStartDate_days']/table[@id='ContentPlaceHolder1_ContentPlaceHolder1_calExtStartDate_daysTable']/tbody[@id='ContentPlaceHolder1_ContentPlaceHolder1_calExtStartDate_daysBody']/tr/td


Datepicker search valid date
${countr} = get element count ${calendardatetable}/tr
log ${countr}
${countr}= evaluate ${countr}+ 1
:FOR ${i} IN RANGE 1 ${countr}
\ ${counttd}= get element count ${calendardatetable}/tr[${i}]/td
\ Datepicker search td date ${i} ${counttd}

Datepicker search td date
[Arguments] ${ctri} ${ctrx}
log ${ctri}
log ${ctrx}
${countx} = get element count ${calendardatetable}/tr[${ctri}]/td
${countx} = evaluate ${countx} + 1
log ${countx}
:FOR ${x} IN RANGE 1 ${countx}
\ ${classsttrib} = get element attribute ${calendardatetable}/tr[${ctri}]/td[${ctrx}] class
\ log ${classsttrib}

I am trying to get the value of the class using get attribute. my test passed but was not able to get the attribute and it returns empty. pls see logs

thanks Pekka.

Best regards,
chris

Capturehtmldatepicker.JPG

To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.
log.html

Tatu Aalto

unread,
Nov 9, 2018, 7:58:37 AM11/9/18
to Christian Rudolf Tan, robotframe...@googlegroups.com
Ugh

The error you receive is coming from Chrome, I would assume that if you change singe quote to double quote: 
//tbody[@id='ContentPlaceHolder1_ContentPlaceHolder1_calExtStartDate_daysBody']/tr[2]/[td][1]
to: 
//tbody[@id="ContentPlaceHolder1_ContentPlaceHolder1_calExtStartDate_daysBody"]/tr[2]/[td][1]

it might work better.

-Tatu

On Fri, 9 Nov 2018 at 14:22, Christian Rudolf Tan <chant...@gmail.com> wrote:
how do you iterate through the elements in the table. when i check my xpath in google chrome it was able to locate the element.
example: 

//div[@class='mainContainer']/table/tbody/tr/td[2]/div/div/div/div[@id='ContentPlaceHolder1_ContentPlaceHolder1_pnlPopup']/div[@class='popUpContent']/table/tbody/tr[5]/td[3]/div[@id='ContentPlaceHolder1_ContentPlaceHolder1_calExtStartDate_container']/div[@id='ContentPlaceHolder1_ContentPlaceHolder1_calExtStartDate_popupDiv']/div[@id='ContentPlaceHolder1_ContentPlaceHolder1_calExtStartDate_body']/div[@id='ContentPlaceHolder1_ContentPlaceHolder1_calExtStartDate_days']/table[@id='ContentPlaceHolder1_ContentPlaceHolder1_calExtStartDate_daysTable']/tbody[@id='ContentPlaceHolder1_ContentPlaceHolder1_calExtStartDate_daysBody']


I am trying to get the value of the class using get attribute.

To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.

To post to this group, send email to robotframe...@googlegroups.com.
Visit this group at https://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.

Christian Rudolf Tan

unread,
Nov 9, 2018, 9:13:06 AM11/9/18
to robotframework-users
Hi Tatu,

I did tried to change from singe quote to double but i still get the same result. it was able to iterate through the table but was not able to get the atrribute class=ajax__calendar_invalid

Hope you can help me.

Best regards,
christian



To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.

To post to this group, send email to robotframe...@googlegroups.com.
Visit this group at https://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.
log.html

Christian Rudolf Tan

unread,
Nov 9, 2018, 4:27:06 PM11/9/18
to robotframework-users
I've corrected my script. it passed but it was not able to get the attribute class.

Datepicker search td date
[Arguments] ${ctri} ${ctrx}
log ${ctri}
log ${ctrx}
${countx} = get element count ${calendardatetable}/tr[${ctri}]/td
${countx} = evaluate ${countx} + 1
log ${countx}
:FOR ${x} IN RANGE 1 ${countx}
         \  check that element exist  ${calendardatetable}/tr[${ctri}]/td[${ctrx}]
\ ${classsttrib}= SeleniumLibrary.Get Element Attribute ${calendardatetable}/tr[${ctri}]/td[${ctrx}] class
\ log ${classsttrib}


Select Datepicker Date
[Documentation] Select given day from datepicker
[Arguments] ${dateElem} ${expectedMonthYear} ${clickElement}
Input Text ${dateElem} ${Empty} # open the datepicker
${monthyear}= Get Datepicker MonthYear
:FOR ${Index} IN RANGE 1 31
\ Run Keyword If '${monthyear}' == '${expectedMonthYear}' Exit For Loop
\ Click Link //*/div[@id='ui-datepicker-div']//*/a[contains(@class, 'ui-datepicker-prev')]
\ ${monthyear}= Get Datepicker MonthYear
Click Link ${clickElement}

i am trying to get know if the attribute of the element which is td that has a class='ajax__calendar invalid'] so that i will skip this and can select a valid date in the date picket.

my code now passed but returns empty

thanks for the help.



On Friday, November 9, 2018 at 7:41:04 PM UTC+8, Pekka Klärck wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages