Handling post_now button and removing cookies window#217
Handling post_now button and removing cookies window#217wkaisertexas merged 11 commits intowkaisertexas:mainfrom
Conversation
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>
|
@claude Hi lol |
| 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()", |
There was a problem hiding this comment.
Isn't there a way to do this with selenium and not using javascript apis?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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}"))
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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}"))
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
|
Can you send a video of this working? |
|
Yep, been working with it for a while and can show you. I'll post it here in a bit. |
|
@S1NJED @wkaisertexas When does the cookie banner appear? I can't reproduce it to test it. |
12-20-35.mp4 |
Signed-off-by: sinjed <oooguuh@gmail.com>
|
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 |
Signed-off-by: sinjed <oooguuh@gmail.com>
post_now, forked the code from Fix #207 #208