pydoll
pydoll copied to clipboard
[Bug]: SyntaxError: Illegal return statement
Checklist before reporting
- [x] I have searched for similar issues and didn't find a duplicate.
- [x] 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
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";
}})();
""")
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
yes that's why i did wrapped it in a function thanks for your efforts
print(await web_element._execute_script('(webElement) => webElement.toDataURL()')) throws an error
print(await web_element.evaluate('(webElement) => webElement.toDataURL()') works with playwright
print(await web_element._execute_script('(function() { return this.toDataURL();})')) works right now