Skip to content
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

[BUG] - playwright._impl._errors.Error: Page.evaluate: ReferenceError: opts is not defined #1172

Open
Feige-cn opened this issue Jul 15, 2024 · 10 comments
Labels
bug Something isn't working

Comments

@Feige-cn
Copy link

Traceback (most recent call last):
File "D:\Python\Git\Tiktok\TikTok-Api\examples\user_example.py", line 23, in
asyncio.run(user_example())
File "C:\Users\H\anaconda3\envs\tiktok\lib\asyncio\runners.py", line 44, in run
return loop.run_until_complete(main)
File "C:\Users\H\anaconda3\envs\tiktok\lib\asyncio\base_events.py", line 641, in run_until_complete
return future.result()
File "D:\Python\Git\Tiktok\TikTok-Api\examples\user_example.py", line 12, in user_example
await api.create_sessions(ms_tokens=[ms_token], num_sessions=1, sleep_after=3, proxies=[{"server": "http://127.0.0.1:15732"}])
File "C:\Users\H\anaconda3\envs\tiktok\lib\site-packages\TikTokApi\tiktok.py", line 260, in create_sessions
await asyncio.gather(
File "C:\Users\H\anaconda3\envs\tiktok\lib\site-packages\TikTokApi\tiktok.py", line 201, in __create_session
await self.__set_session_params(session)
File "C:\Users\H\anaconda3\envs\tiktok\lib\site-packages\TikTokApi\tiktok.py", line 94, in __set_session_params
user_agent = await session.page.evaluate("() => navigator.userAgent")
File "C:\Users\H\anaconda3\envs\tiktok\lib\site-packages\playwright\async_api_generated.py", line 8340, in evaluate
await self._impl_obj.evaluate(
File "C:\Users\H\anaconda3\envs\tiktok\lib\site-packages\playwright_impl_page.py", line 440, in evaluate
return await self._main_frame.evaluate(expression, arg)
File "C:\Users\H\anaconda3\envs\tiktok\lib\site-packages\playwright_impl_frame.py", line 278, in evaluate
await self._channel.send(
File "C:\Users\H\anaconda3\envs\tiktok\lib\site-packages\playwright_impl_connection.py", line 59, in send
return await self._connection.wrap_api_call(
File "C:\Users\H\anaconda3\envs\tiktok\lib\site-packages\playwright_impl_connection.py", line 514, in wrap_api_call
raise rewrite_error(error, f"{parsed_st['apiName']}: {error}") from None
playwright._impl._errors.Error: Page.evaluate: ReferenceError: opts is not defined
at Navigator.get [as userAgent] (:11:42)
at eval (eval at evaluate (:226:30), :1:17)
at UtilityScript.evaluate (:233:19)
at UtilityScript. (:1:44)

@Feige-cn Feige-cn added the bug Something isn't working label Jul 15, 2024
@mitramir55
Copy link

Same issue. Here's the code:

import asyncio
import os

ms_token = os.environ.get("ms_token", None)  # set your own ms_token


async def get_hashtag_videos():
    async with TikTokApi() as api:
        await api.create_sessions(ms_tokens=[ms_token], num_sessions=1, sleep_after=3)
        tag = api.hashtag(name="donut")
        async for video in tag.videos(count=3):
            print(video)
            print(video.as_dict)


if __name__ == "__main__":
    try:
        asyncio.run(get_hashtag_videos())
    except Exception as e:
        print(f"An error occurred: {e}")

And the error:

An error occurred: Page.evaluate: ReferenceError: opts is not defined
    at Navigator.get [as userAgent] (<anonymous>:11:42)
    at eval (eval at evaluate (:226:30), <anonymous>:1:17)
    at UtilityScript.evaluate (<anonymous>:233:19)
    at UtilityScript.<anonymous> (<anonymous>:1:44)

@Dagmawi-Beyene
Copy link

I had the same issue,

A quick fix is to downgrade playwright using pip install playwright==1.37.0 and run playwright install

@idrinkandiknowthing
Copy link

I had the same issue,

A quick fix is to downgrade playwright using pip install playwright==1.37.0 and run playwright install

Thanks bro, this can resolve my issue

@maxcohen55
Copy link

Note that this fix is not yet supported for python 3.12. I had to downgrade to 3.11.x to get this to work.

@n0rc
Copy link

n0rc commented Aug 1, 2024

For me python 3.12 works when using playwright==1.39.0.

@cmagsisi
Copy link

cmagsisi commented Aug 3, 2024

Can confirm python 3.12 works with playwright==1.39.0.

@renzaijianghu1
Copy link

我的Python是Python 3.11.7,playwright==1.37.0为何还是不行,我运行时它自动打开tiktok不到2秒就自动关闭了

@stephenarosaj
Copy link

can also confirm as of 8/12/24 python 3.12 works with playwright==1.39.0 - thank you @n0rc <3

@grIskra
Copy link

grIskra commented Aug 18, 2024

is this API not updated anymore?

@davidteather
Copy link
Owner

This is a weird issue, I can't seem to figure it out. But it works if you specify webkit as your browser
If anyone else looks into it, I've found this

user_agent = await session.page.evaluate("() => navigator.userAgent")

this throws the error

user_agent = await session.page.evaluate("() => navigator.userAgentData")

Doesn't, and most other attribute accesses seem to work fine. It feels like a weird way that playwright must've changed handling this in 1.4.0

But this script works normally so it might be an issue on how this library is assigning pages?

import asyncio
from playwright.async_api import async_playwright

async def run():
    async with async_playwright() as playwright:
        browser = await playwright.chromium.launch(headless=True)  # Set headless=True to run without UI
        page = await browser.new_page()
        await page.goto('https://tiktok.com')

        user_agent = await page.evaluate("() => navigator.userAgent")
        print(user_agent)

        await page.wait_for_timeout(5000)  
        await browser.close()

asyncio.run(run())

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