-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselenium_youtube.py
106 lines (82 loc) · 2.56 KB
/
selenium_youtube.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# Using Python and Selenium you can:
# turn on browser and YouTube,
# search for a song,
# turn on/off subtitles,
# pause/turn off a song whenever you want
# close the browser
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
def turn_on_yt():
browser.get('https://www.youtube.com')
browser.maximize_window()
browser.implicitly_wait(15)
# accept cookies policy
cookies = browser.find_element(By.CSS_SELECTOR,
"[aria-label='Accept the use of cookies and other data for the purposes described']")
cookies.click()
def search_clip(name):
search_form = browser.find_element(By.NAME, "search_query")
search_form.send_keys(name)
search_form.submit()
def turn_on_first_clip():
for i in range(4):
time.sleep(4)
try:
result = browser.find_elements(By.ID, 'video-title')
print(result)
result[0].click()
break
except Exception as e:
print(e)
def info_banner():
banner = browser.find_element(By.CSS_SELECTOR, "[aria-label='No thanks']")
banner.click() # close yt info banner
def subtitles():
turn_on_off = browser.find_elements(By.CLASS_NAME, "ytp-subtitles-button")[0]
turn_on_off.click() # turn on/off subtitles
def switch_to_fullscreen():
for i in range(10):
time.sleep(1)
try:
fullscreen = browser.find_elements(By.CLASS_NAME, 'ytp-fullscreen-button')[0]
fullscreen.click() # same button to minimize screen
break
except Exception as e:
print(e)
# def write_down_time():
# for x in range(20):
# time.sleep(1)
# try:
# c_time = browser.find_elements(By.CLASS_NAME, "ytp-time-current")[0]
# time.sleep(12)
# print(c_time.text)
# c_time[0].click()
# time.sleep(5)
# browser.close()
# except Exception as e:
# print(e)
def pause():
time.sleep(15)
player = browser.find_elements(By.CLASS_NAME, 'ytp-play-button')[0]
player.click()
if __name__ == "__main__":
browser = webdriver.Firefox()
turn_on_yt()
time.sleep(3)
search_clip('imagine dragons sharks')
turn_on_first_clip()
time.sleep(35) # wait for the ads to end
info_banner()
switch_to_fullscreen()
# subtitles()
time.sleep(10)
pause()
subtitles()
time.sleep(2)
switch_to_fullscreen() # minimize screen
time.sleep(2)
print('done')
browser.close()
time.sleep(2)
quit()