Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mqu
Created November 11, 2017 08:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mqu/dfd78ea45dad2072da6e68d3158ce0d1 to your computer and use it in GitHub Desktop.
Save mqu/dfd78ea45dad2072da6e68d3158ce0d1 to your computer and use it in GitHub Desktop.
selenium and youtube player
#!/usr/bin/ruby
require "selenium-webdriver"
require 'pp'
# install :
# sudo apt-get install ruby ruby-dev chromium-chromedriver firefoxdriver
# sudo gem install selenium-webdriver
# sudo ln -s /usr/lib/chromium-browser/chromedriver /usr/local/bin/
# usefull links :
# https://gist.github.com/kenrett/7553278
# http://www.assertselenium.com/webdriver/getting-started-with-ruby-webdriver/
# python script : https://gist.github.com/sweetmoniker/58c63c6c107384bb642524f0ae1c9a48
# here, select one youtube video URL
url='https://www.youtube.com/watch?v=XXXXXXX'
# xpath (and css) selectors to get information on HTML content in page.
path={}
# view counter
path[:counter]='/html//yt-view-count-renderer'
path[:title]='/html//ytd-video-primary-info-renderer/div/h1'
path[:likes]='ytd-toggle-button-renderer.style-scope:nth-child(1) > a:nth-child(1) > yt-formatted-string:nth-child(2)'
path[:duration]='/html/body/ytd-app/div[1]/ytd-page-manager/ytd-watch/div[1]/div[1]/div[1]/div/div[20]/div[2]/div[1]/div/span[3]'
# title
# /html/body/ytd-app/div[1]/ytd-page-manager/ytd-watch/div[1]/div[2]/div/div[6]/div[2]/ytd-video-primary-info-renderer/div/h1
# likes (css):
# ytd-toggle-button-renderer.style-scope:nth-child(1) > a:nth-child(1) > yt-formatted-string:nth-child(2)
# duration :
# /html/body/ytd-app/div[1]/ytd-page-manager/ytd-watch/div[1]/div[1]/div[1]/div/div[20]/div[2]/div[1]/div/span[3]
# configure the driver to run in headless mode
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')
options.add_argument('--incognito')
options.add_argument('--mute-audio')
driver = Selenium::WebDriver.for :chrome, options: options
#Loading the assertselenium URL
driver.navigate.to url
# Timeout = 15 sec
wait = Selenium::WebDriver::Wait.new(:timeout => 15)
wait.until {
/https:\/\/www.youtube.com\//.match(driver.page_source)
views=driver.find_elements(:xpath, path[:counter]).first.text
title=driver.find_elements(:xpath, path[:title]).first.text
likes=driver.find_elements(:css, path[:likes]).first.text
duration=driver.find_elements(:xpath, path[:duration]).first.text
puts "views: #{views} ; likes: #{likes} ; len:#{duration} - #{title}"
#read for some time video
sleep 15
#Quitting the browser
driver.quit
exit 0
}
throw exception "could not open url : #{ur}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment