RobotFramework Python Get Text from inside a javascript function

Viewed 63

The below alert text inside the pasted function below only appears when I hit a button. I wish to capture the text to validate it.

When I use the following code I can find the locator on the page, but no text is returned in my response. How can I get inside the function?

${response}    Get Text    css=body script:nth-child(9)

(script)
        $(function () {
            Metro.toast.create("No changes have been entered so 
                   nothing will be updated in CMS!", null, 6000, "alert");
        });
 (/script)     

The response comes as pass but with no text.

15:49:17.350 INFO ${response} =

1 Answers

Get Text in Selenium/SeleniumLibrary returns the visible text in a tag - e.g. what the browser renders in the viewport. Naturally, anything that is in a <script> is not that - it's code that will be executed (regardless what it'll do - put some text out there or not, it is code).

You can still get the tag's content though - every DOM element has an attribute innerHTML, that is its raw content (before rendering and so on). So try this:

${response}    Get Element Attribute   css=body script:nth-child(9)    innerHTML

This will of course return you everything (because the response is the raw content) - "Metro.toast.create(", etc - so you'll have to manipulate the output a bit, to get just the method's argument.

Related