Reputation: 105
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
Upvotes: 0
Views: 1227
Reputation: 1
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)
Upvotes: 0
Reputation: 44323
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()
Upvotes: 0
Reputation: 199
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.
Upvotes: 1