0

I am trying to make Python play a video which is embedded in a website (https://harsh10822.wixsite.com/intro). I tried using ID,xpath etc etc but It didn't workout. There is a similar Question to mine here (How to click on the play button of a youtube video embedded within smtebook through selenium and python) But I couldn't figure out how to apply the code. If You could help me out by providing the code,I will be really happy. Thankyou

3 Answers 3

1

This should work.

driver = webdriver.Firefox()
driver.get('https://harsh10822.wixsite.com/intro')
time.sleep(5)
video = driver.find_element_by_id('video-player-comp-ka1067od')
video.click()

Waiting is important in this case, because embedded video doesn't load instantly with the page so selenium needs to wait. You can change 5 seconds to any number that works for you.

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

6 Comments

Or you can issue a driver.implicitly_wait(5) right after the driver is initialized, don't issue a call to sleep, and then the call to find_element_by_id should wait for up to 5 seconds before it times out. But if the element appears before 5 seconds, you will not be waiting extra time needlessly. So you might as well make the number of seconds arbitrarily large.
That would be ideal, but in this case page finishes loading before you can play the video. In other words page loads first and after page finishes loading the video is loaded. So the only solution I can see is with time.sleep() method.
I don't believe clicking on that id works. And it's a question of just waiting for the correct elements to load.
Code that I posted works, but if you have another idea I would love to see it.
And I should have said that I did try your answer a second time and it did work and so I upvoted it,
|
0

See: How to click on the play button of a youtube video embedded within smtebook through selenium and python

But this uses a slightly different technique instead of WebDriverWait or sleeping for a fixed number of seconds, which can be wasteful if you are sleeping for longer than necessary.

Instead, use a call to driver.implicitly_wait(10). Then any calls that attempt to locate elements will implicitly wait for up to 10 seconds for the element to appear before timing out but will wait no longer than necessary. It's just a question of knowing which elements to look for and where:

driver = webdriver.Firefox()

driver.implicitly_wait(10) # wait for up to 10 seconds before timeout for any find operation

driver.get('https://harsh10822.wixsite.com/intro')
driver.switch_to.frame(driver.find_element_by_xpath('//iframe[starts-with(@src, "https://www.youtube.com/embed")]')) # switch to iframe
button = driver.find_element_by_xpath('//button[@aria-label="Play"]') # look for button
button.click()

Comments

0

Make sure that particular web driver is downloaded and placed in the directory wherein your code is saved. And for reference you can refer to Tech With Tim selenium tutorials.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
video=input()
driver=webdriver.Chrome()
driver.get("https://youtube.com")
search=driver.find_element_by_id("search")
time.sleep(2)
search.send_keys(video,Keys.RETURN)
time.sleep(2)
play=driver.find_element_by_id("video-title")
play.send_keys(Keys.RETURN)

Comments

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.