1

I'm trying to create a Python3 script using selenium that opens a youtube video, then skips the add. I tried using this:

self.driver.find_element_by_xpath('//*[@id="skip-button:8"]/span/button')\
            .click()

But it would just pause the video, because I think the mouse needs to be hovering the button before clicking it. Here is the class that will do this:

class MusicPlayer():
    def __init__(self):
        self.driver = webdriver.Safari()
        self.driver.get("https://www.youtube.com/watch?v=suia_i5dEZc")
        sleep(10)
        self.driver.find_element_by_xpath('//*[@id="skip-button:8"]/span/button')\
            .click()
        sleep(260)
1

2 Answers 2

1
button = driver.find_element_by_class_name('ytp-ad-skip-button-container')
button.click()

Is the way I figured to do it.

Sign up to request clarification or add additional context in comments.

Comments

1

you will have to use WebDriverWait to wait for the element until it is clickable

driver = webdriver.Chrome()
driver.get("https://www.youtube.com/watch?v=suia_i5dEZc")


wait = WebDriverWait(driver, 20*60)
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='skip-button:6']/span/button")))

element.click()

1 Comment

from selenium.webdriver.support import expected_conditions as EC

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.