Skip to content

Handling post_now button and removing cookies window#217

Merged
wkaisertexas merged 11 commits intowkaisertexas:mainfrom
S1NJED:fix
Sep 3, 2025
Merged

Handling post_now button and removing cookies window#217
wkaisertexas merged 11 commits intowkaisertexas:mainfrom
S1NJED:fix

Conversation

@S1NJED
Copy link
Copy Markdown
Contributor

@S1NJED S1NJED commented Aug 26, 2025

  • Handling the new button post_now, forked the code from Fix #207 #208
  • Removed cookies window instead of clicking on decline button that can cause crash because of shadow root

DALIHILLARY and others added 8 commits August 10, 2025 03:37
Signed-off-by: sinjed <oooguuh@gmail.com>
Signed-off-by: sinjed <oooguuh@gmail.com>
Signed-off-by: sinjed <oooguuh@gmail.com>
Signed-off-by: sinjed <oooguuh@gmail.com>
Signed-off-by: sinjed <oooguuh@gmail.com>
Signed-off-by: sinjed <oooguuh@gmail.com>
@BlindMaster24
Copy link
Copy Markdown

@claude Hi lol

@BlindMaster24
Copy link
Copy Markdown

@wkaisertexas ?

Comment thread src/tiktok_uploader/upload.py Outdated
decline_button = WebDriverWait(driver, config.implicit_wait).until(
EC.element_to_be_clickable(item.find_elements(By.TAG_NAME, "button")[0])
driver.execute_script(
"document.querySelector(arguments[0]).remove()",
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there a way to do this with selenium and not using javascript apis?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no unfortunately, thats why i used a script, but my solution is just a simpler way to remove the cookies banner, we can still press the decline button ig

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there a way to do this with selenium and not using javascript apis?

from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

from tiktok_uploader import config, logger
from tiktok_uploader.utils import green, red

def _remove_cookies_window(driver: WebDriver) -> None:
"""
Removes the cookies window if it is open by accessing the shadow DOM.

Parameters
----------
driver : selenium.webdriver
"""
logger.debug(green("Removing cookies window"))
try:
    # Wait for the shadow host (the banner) to be present
    shadow_host = WebDriverWait(driver, config.implicit_wait).until(
        EC.presence_of_element_located(
            (By.TAG_NAME, config.selectors.upload.cookies_banner.banner)
        )
    )

    # Access the shadow root
    shadow_root = shadow_host.shadow_root

    # Find the button wrapper inside the shadow root
    button_wrapper = shadow_root.find_element(
        By.CSS_SELECTOR, config.selectors.upload.cookies_banner.button
    )

    # Find the actual button within the wrapper (usually the first one is decline/reject)
    decline_button = button_wrapper.find_element(By.TAG_NAME, "button")

    # Wait for the button to be clickable and then click it
    WebDriverWait(driver, config.implicit_wait).until(
        EC.element_to_be_clickable(decline_button)
    )
    decline_button.click()

    logger.debug(green("Clicked the decline button in the cookies banner."))

except TimeoutException:
    logger.debug(red("Cookies banner not found or button not clickable in time."))
except Exception as e:
    logger.error(red(f"An unexpected error occurred while removing cookies window: {e}"))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean this is practically the same code as before, i decided to not use shadow root because it crash sometimes or maybe we can keep this logic and when it crash we try to remove it using the js script ?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there a way to do this with selenium and not using javascript apis?

from selenium.webdriver.remote.webdriver import WebDriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException

from tiktok_uploader import config, logger from tiktok_uploader.utils import green, red

def _remove_cookies_window(driver: WebDriver) -> None: """ Removes the cookies window if it is open by accessing the shadow DOM.

Parameters
----------
driver : selenium.webdriver
"""
logger.debug(green("Removing cookies window"))
try:
    # Wait for the shadow host (the banner) to be present
    shadow_host = WebDriverWait(driver, config.implicit_wait).until(
        EC.presence_of_element_located(
            (By.TAG_NAME, config.selectors.upload.cookies_banner.banner)
        )
    )

    # Access the shadow root
    shadow_root = shadow_host.shadow_root

    # Find the button wrapper inside the shadow root
    button_wrapper = shadow_root.find_element(
        By.CSS_SELECTOR, config.selectors.upload.cookies_banner.button
    )

    # Find the actual button within the wrapper (usually the first one is decline/reject)
    decline_button = button_wrapper.find_element(By.TAG_NAME, "button")

    # Wait for the button to be clickable and then click it
    WebDriverWait(driver, config.implicit_wait).until(
        EC.element_to_be_clickable(decline_button)
    )
    decline_button.click()

    logger.debug(green("Clicked the decline button in the cookies banner."))

except TimeoutException:
    logger.debug(red("Cookies banner not found or button not clickable in time."))
except Exception as e:
    logger.error(red(f"An unexpected error occurred while removing cookies window: {e}"))

@S1NJED

Copy link
Copy Markdown

@BlindMaster24 BlindMaster24 Aug 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean this is practically the same code as before, i decided to not use shadow root because it crash sometimes or maybe we can keep this logic and when it crash we try to remove it using the js script ?

Normally, there's no problem with it. The shadow root is the recommended way now with Selenium 4>. Let's try using the shadow root first, and then as a fallback, we can remove it if it fails. But tbh, using a JS script isn't recommended anymore, shadow root is the right way to go.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean this is practically the same code as before, i decided to not use shadow root because it crash sometimes or maybe we can keep this logic and when it crash we try to remove it using the js script ?

Ok, let's go with 2 options then. We can ditch the JS later if we find out shadow root it's 100% stable, since it's just redundant code anyway. I mean, we could run like 150 checks lol but that would be overkill. It needs testing. For now, I suggest making shadow root the first attempt.
And yeah, this isn't the same as shadow root. If you're interested, read up on why this method was introduced.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my case, remove_cookie window using shadow root sometimes crash (for unknown reason) and the reason is shadow root is undefined something like that, so we can keep the shadow root logic and if it raise an error like not found we use the js script as a fallback

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my case, remove_cookie window using shadow root sometimes crash (for unknown reason) and the reason is shadow root is undefined something like that, so we can keep the shadow root logic and if it raise an error like not found we use the js script as a fallback

Sounds good, let's do it that way. I'll test it on my side and I'll remove the js code once I see it's working for sure, since using js isn't recommended like I said before, at least for the sake of clean code.

@wkaisertexas
Copy link
Copy Markdown
Owner

Can you send a video of this working?

@BlindMaster24
Copy link
Copy Markdown

Yep, been working with it for a while and can show you. I'll post it here in a bit.
@S1NJED pls apply my change.

@BlindMaster24
Copy link
Copy Markdown

@S1NJED @wkaisertexas When does the cookie banner appear? I can't reproduce it to test it.

@S1NJED
Copy link
Copy Markdown
Contributor Author

S1NJED commented Aug 28, 2025

12-20-35.mp4

Signed-off-by: sinjed <oooguuh@gmail.com>
@S1NJED
Copy link
Copy Markdown
Contributor Author

S1NJED commented Aug 29, 2025

So instead of relying only on a js script i first try to remove it by accessing the shadow root (the same code as before) and catch the error and remove it using a js script

Here is the error i get sometimes when accessing shadow root

22:33:20] Authenticating browser with cookies
[22:33:20] Create a chrome browser instance 
[22:33:21] Authenticating browser with cookies
[22:33:27] Posting upload.mp4
               with description: This is the description
[22:33:27] Navigating to upload page
[22:33:29] Removing cookies window
Traceback (most recent call last):
  File "/home/sinjed/cloned/tiktok-uploader/src/tiktok_uploader/upload.py", line 534, in _remove_cookies_window
    cookies_banner.shadow_root.find_element(
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sinjed/cloned/tiktok-uploader/.venv/lib/python3.12/site-packages/selenium/webdriver/remote/webelement.py", line 328, in shadow_root
    return self._execute(Command.GET_SHADOW_ROOT)["value"]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sinjed/cloned/tiktok-uploader/.venv/lib/python3.12/site-packages/selenium/webdriver/remote/webelement.py", line 573, in _execute
    return self._parent.execute(command, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sinjed/cloned/tiktok-uploader/.venv/lib/python3.12/site-packages/selenium/webdriver/remote/webdriver.py", line 454, in execute
    self.error_handler.check_response(response)
  File "/home/sinjed/cloned/tiktok-uploader/.venv/lib/python3.12/site-packages/selenium/webdriver/remote/errorhandler.py", line 232, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchShadowRootException: Message: no such shadow root: shadow root not found

S1NJED and others added 2 commits August 29, 2025 22:41
Signed-off-by: sinjed <oooguuh@gmail.com>
@wkaisertexas wkaisertexas merged commit 40f3a24 into wkaisertexas:main Sep 3, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants