Skip to content

[Bug]: SyntaxError: Illegal return statement #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
2 tasks done
dragonscraper opened this issue Apr 27, 2025 · 3 comments
Open
2 tasks done

[Bug]: SyntaxError: Illegal return statement #124

dragonscraper opened this issue Apr 27, 2025 · 3 comments
Labels
bug Something isn't working

Comments

@dragonscraper
Copy link

Checklist before reporting

  • I have searched for similar issues and didn't find a duplicate.
  • I have updated to the latest version of pydoll to verify the issue still exists.

pydoll Version

1.7.0

Python Version

3.12

Operating System

Windows

Bug Description

when i run

() => { const finalBtn = document.querySelector('div.contact-boxes.ng-star-inserted'); if (finalBtn) { return finalBtn.innerHTML; } else { return "Element not found"; } }
i get

{'id': 19, 'result': {'result': {'type': 'object', 'subtype': 'error', 'className': 'SyntaxError', 'description': 'SyntaxError: Illegal return statement', 'objectId': '7549169250272292091.6.2'}, 'exceptionDetails': {'exceptionId': 1, 'text': 'Uncaught', 'lineNumber': 2, 'columnNumber': 36, 'scriptId': '40', 'exception': {'type': 'object', 'subtype': 'error', 'className': 'SyntaxError', 'description': 'SyntaxError: Illegal return statement', 'objectId': '7549169250272292091.6.3'}}}}

Steps to Reproduce

i run

await page.execute_script("""
                                       () => {
            const finalBtn = document.querySelector('div.contact-boxes.ng-star-inserted');
            if (finalBtn) {
                return finalBtn.innerHTML;
            } else {
                return "Element not found";
            }
        }
                                    """)

i even tried the one in docs
title = await page.execute_script('return document.title')

Code Example

async with Chrome() as browser:
        await browser.start()
        page = await browser.get_page()
        await page.go_to(url)

        # Extract data
        title = await page.execute_script('return document.title')

Expected Behavior

No response

Actual Behavior

No response

Relevant Log Output

Additional Context

No response

@dragonscraper dragonscraper added the bug Something isn't working label Apr 27, 2025
@yie1d
Copy link
Contributor

yie1d commented Apr 29, 2025

hi @dragonscraper , You can use execute_script to achieve all commands that can be done in the console.:

async def main():
    async with Chrome() as browser:
        await browser.start()
        page = await browser.get_page()

        await page.go_to('https://github.com/')
        title = await page.execute_script("""document.title""")
        result = await page.execute_script("""
        (function() {             
            const finalBtn = document.querySelector('div.contact-boxes.ng-star-inserted');
            if (finalBtn) {
                return finalBtn.innerHTML;
            } else {
                return "Element not found";
            }})();
        """)

@thalissonvs
Copy link
Member

Hi @dragonscraper,

This is a known bug that will be fixed in the next release. For now, execute_command can only run the same code you'd be able to execute in the browser console. So, to use return, you'll need to place your code inside a function. I ask for a bit of patience, his will become more intuitive soon

@dragonscraper
Copy link
Author

yes that's why i did wrapped it in a function thanks for your efforts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants