davidteather / TikTok-Api

The Unofficial TikTok API Wrapper In Python
https://davidteather.github.io/TikTok-Api
MIT License
4.86k stars 976 forks source link

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

Open Feige-cn opened 4 months ago

Feige-cn commented 4 months ago

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)

mitramir55 commented 4 months ago

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 commented 4 months ago

I had the same issue,

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

idrinkandiknowthing commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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

cmagsisi commented 3 months ago

Can confirm python 3.12 works with playwright==1.39.0.

renzaijianghu1 commented 3 months ago

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

stephenarosaj commented 3 months ago

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

grIskra commented 2 months ago

is this API not updated anymore?

davidteather commented 2 months ago

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())
wh1t3h47 commented 3 days ago

I think this is a playwright specific error, the issue should be posted there