Get text from css element returns no results

Hello everyone,

I am trying to save the value (number) behind the class=“case_title”.

Original code:
00303712

My code:

Open Browser    ${URL}    browser=chrome
${element}=    Get Text    css=.case_title
Set Global Variable    ${element}
Log    ${element}

I am not able to solve this problem. Maybe someone can help me.

Best regards

Hi @SteL,

A quick tip, FYI - use three back ticks (```) before and after any html or code so to doesn’t get messed up.

What is the result of your code?:

  • does it error on the Get Text step?
    • what was the error?
  • does it get to the Log step?
    • what does it log?

I would start by checking the html if there is more than one element that matches you css selector (especially ones that are in div’s that are disabled or hidden)

Hopefully that’s helpful,

Dave.

Hi @damies13,

thanks for the tip. I noticed that I have forgot my html code.

00303712

So I wanted to store the number in a variable using the code I mentioned earlier. I didn’t get any errors, but my variable is always empty.

Hi @SteL,

The fact you got a blank indicates that there is more than one element that matched that css selector and the first one it matched is not the one you want.

Go back to the html of the page and see if you can find them all and try and work out a css selector that will uniquely find the span you want

It may be as simple as .case-id .case_title or span.case_title, but it might also be more involved than that.

If you still can’t figure it out you could try adding something like this before the Get Text line:

# this should show you if you get more than one
${webelements}=    Get WebElements    css=.case_title
Log    ${webelements} 
# this will give you more info on what you selected
${webelement}=    Get WebElement    css=.case_title
Log    ${webelement} 
Log    ${webelement.get_attribute('innerHTML')} 
Log    ${webelement.get_attribute('outerHTML')} 

That should help you identify if in fact you are selecting the wrong case_title element

Dave.

You could try to use the id, if it is static for the run of the testcase
So
${number} get text id=201905…
and if it works then maybe making another CSS/xpath selector
${number} get text css=[class=“case_title”]
${number} get text css=span[data-template]
${number] get text //span[@class=“case_title”]

@damies13

I checked several variants of case-id, case_title, and so on. There is only one case_title available.

I tried your code and if I am interpret it correctly then there is only one element available and the result of the inner and outer html is:

outer: <span data-template="" data-test-id="20190510022618055338234" class="case_title"></span>

inner: empty

Interesting behavior here. Then I’ll probably have to delve deeper into the subject. I’m still relatively new to the subject of robotframework

Unfortunately the number is not a static value. This is number is always new generated in case of creating new cases with our tool. So I have to save this number to proceed further in our process. There are one workaround which I can use but I would like to avoid this, however, because I might then select the wrong case

Hi @SteL,

This looks to me like it might be the value is empty when the page is loaded from the server and gets filled in by some client side java script, in that case you might need to wait for the java script to finish first.

See if you can use one of the Wait Until * keywords (maybe on another element?) to determine that it’s finished

You may also need to try using Execute Javascript to check document.readyState returns “complete”

Failing all that as a last resort use a simple loop like this:

${element}=    Get Text    css=.case_title
WHILE    len(${element}) < 1
    Sleep    0.1
    ${element}=    Get Text    css=.case_title
END
Set Global Variable    ${element}
Log    ${element}

Be warned that could leave you in an endless loop so you may need to put a counter with a break point in that loop. Those Wait Until keywords already have the endless loop protections in them, so they should be your first resort.

Hopefully one of those gets you the value,

Dave.

2 Likes

Thank you for these tips. I was able to solve my problem thanks to the Java Script info. I simply waited until the script was finished or I simply put the query at the end. Thanks again

1 Like