TraySimpson / RedditVideoGenerator

334 stars 135 forks source link

Screenshot Cookie #15

Closed kieranperk closed 1 year ago

kieranperk commented 1 year ago

I've managed to get my system all working, however whenever it goes to take screenshots I have to quickly manually click the cookie accept button every single time I want to create a video which can get tricky when it only gives you like a second until it starts to take all of the screenshots. Any ideas on what I could do?

Example of the screenshots it gives me: 2023-04-05-12c3715-t1_jeztj60 2023-04-05-12c3715-t1_jeztzxj

Delafinn commented 1 year ago

what browser do you use? do you use adblocker at all?

rborsetti commented 1 year ago

You can use the click() method of the WebElement class within selenium. You will just need to find the element.

ItsLeon15 commented 1 year ago

@kieranperk One method you could try is by clicking the element that contains the text 'Accept all'. It is a lot easier than getting the ID of the element. Below is an example but it does slow the process down. By all means you could try another approach but this one seemed to work for me, it doesn't work all the time and has its issues.

# screenshot.py

def getPostScreenshots(filePrefix, script):
    print("Taking screenshots...")
    driver, wait = __setupDriver(script.url)
    script.titleSCFile = __takeScreenshot(filePrefix, driver, wait) 
    for commentFrame in script.frames:
        try:
            button = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[contains(text(),'Accept all')]")))
            button.click()
        except:
            pass
        commentFrame.screenShotFile = __takeScreenshot(filePrefix, driver, wait, f"t1_{commentFrame.commentId}")
    driver.quit()
kieranperk commented 1 year ago

@kieranperk One method you could try is by clicking the element that contains the text 'Accept all'. It is a lot easier than getting the ID of the element. Below is an example but it does slow the process down. By all means you could try another approach but this one seemed to work for me, it doesn't work all the time and has its issues.

# screenshot.py

def getPostScreenshots(filePrefix, script):
    print("Taking screenshots...")
    driver, wait = __setupDriver(script.url)
    script.titleSCFile = __takeScreenshot(filePrefix, driver, wait) 
    for commentFrame in script.frames:
        try:
            button = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[contains(text(),'Accept all')]")))
            button.click()
        except:
            pass
        commentFrame.screenShotFile = __takeScreenshot(filePrefix, driver, wait, f"t1_{commentFrame.commentId}")
    driver.quit()

@ItsLeon15 Thank you very much! I added the code to the __setupDriver() function so it ran asap and only once and it seems to work really well!

def __setupDriver(url: str):
    options = webdriver.FirefoxOptions()
    options.headless = False
    options.enable_mobile = False
    driver = webdriver.Firefox(options=options)
    wait = WebDriverWait(driver, 10)

    driver.set_window_size(width=screenWidth, height=screenHeight)
    driver.get(url)

    try:
        button = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[contains(text(),'Accept all')]")))
        button.click()
    except:
        print("No 'Accept all' button found on the page")