0

I am building a bot in Python Selenium which can enroll in Free Highest rated courses on Udemy. I also want the bot to play videos for me and when the video gets finished, it can then move on to the next video. Previously i searched on Google and found some of the Answers of how to get a video length of a video file but these are not working on Udemy Course Video..

What i actually want is to get the total length of a video in seconds or minutes while the bot will continuously keep track of the time and when the played time becomes equal to the total length of a video, it can then move on to the next video...Below is the code that i am using which will only Log in to the account and Enroll in one of the Free Searched Course on Udemy.

from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver

email='huaaddty8339@gmail.com'
password='kulningkswoymama7'

browser = webdriver.Firefox()
browser.get('https://www.udemy.com')
time.sleep(25)
browser.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div[4]/div[6]').click()
time.sleep(9)

browser.find_element_by_xpath('//*[@id="email--1"]').send_keys(email)
time.sleep(4)

browser.find_element_by_xpath('//*[@id="id_password"]').send_keys(password)
time.sleep(2)

browser.find_element_by_xpath('//*[@id="submit-id-submit"]').click()
time.sleep(16)

browser.find_element_by_xpath('//*[@id="header-search-field"]').send_keys("Python")
time.sleep(15)

WebDriverWait(browser,20).until(EC.element_to_be_clickable((By.XPATH,"//button[.//span[text()='All 
Filters']]"))).click()


time.sleep(5)
WebDriverWait(browser,20).until(EC.element_to_be_clickable((By.XPATH,"//label[@title and 
.//span/span[text()='Free']]"))).click()
time.sleep(5)

browser.execute_script("window.open('');") 
time.sleep(5)
browser.switch_to.window(browser.window_handles[1])
time.sleep(5)
browser.get("https://www.udemy.com/course/kypython-101/")
time.sleep(30)
try:

 browser.find_element_by_xpath("//html/body/div[2]/div[3]/div[3]/div[1]/div[2]/div[3]/div[1]/div/div/button").click()
 time.sleep(3)

except:
 WebDriverWait(browser, 30).until(EC.element_to_be_clickable((By.XPATH,"/html/body/div[2]/div[3]/div[4]/div/div[2]/div[2]/div/div[1]/div[2]/div[1]/div/div[5]/div/div/div/button"))).click()
Joe Ann
  • 15
  • 6

1 Answers1

0

You can do the following:

  1. Parse id of the video tag that holds required video
  2. Execute javascript code within a page like document.getElementById('playerId__4399364_html5_api').duration which will return duration in seconds. Do not forget to change id to one relevant to your case.

P.S. - You can learn more about JS execution here: Running javascript in Selenium using Python

P.P.S. - Here you can find more properties of video which migh help you in your tasks.

Alexey R.
  • 8,057
  • 2
  • 11
  • 27