at first Since this is my first post, I think there are many things that are missing, but please watch with warm eyes. In addition, since I am a beginner, I think that some parts of the code created by exploration may be difficult to understand, but please understand.
・ Operate the web using selenium
__ To the main subject __
Selenium is the one that can operate web pages with python. In this article, I will omit the environment construction and basic usage for operating Chrome via selenium with python.
__Import webdriver from selenium __
For the time being, let's import the necessary modules
python:netflix.py
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.action_chains import ActionChains
import time
When running the Web, import the time module to rest so that you do not specify the element before going to the next page.
python:netflix.py
driver = webdriver.Chrome("Specify the location where you saved the chrome driver")
#(Example) driver= webdriver.Chrome("C:\Command\chromedriver")
driver.get('https://www.netflix.com/jp/')
time.sleep(1)
Now you can access the Netflix page for now. Of course, as an application, you can access other web pages by changing the part of https://www.netflix.com/jp/.
Enter characters by specifying __Path __ Next, write the code that lets you enter characters.
python:netflix.py
login = driver.find_element_by_css_selector('#appMountPoint > div > div > div > div > div > div.our-story-header-wrapper > div > a')
login.click()
#Go to login screen
driver.find_element_by_name('userLoginId').send_keys('mail address')
driver.find_element_by_name('password').send_keys('password')
#Enter your email address and password
driver.find_element_by_css_selector('#appMountPoint > div > div.login-body > div > div > div.hybrid-login-form-main > form > button').click()
time.sleep(1)
At this point, you're logged in to Netflix. However, from here on, the Path you specify depends on your Netflix contract plan, so I'll write the code in my case. As a matter of fact, if the code is written so far, you can specify the path and click, specify the path and input to go to automatic playback.
__ Until playback after login is completed __
Select yourself from here and repeat by specifying, clicking, and inputting the path until automatic playback.
python:netflix.py
driver.find_element_by_xpath('//*[@id="appMountPoint"]/div/div/div[1]/div[1]/div[2]/div/div/ul/li[1]/div/a/div/div').click()
time.sleep(1)
#Choose yourself
driver.find_element_by_xpath('//*[@id="appMountPoint"]/div/div/div[1]/div[1]/div[1]/div/div/div/div[1]/div/button/span').click()
#Click the search field
title ="Kengan Ashura"
driver.find_element_by_xpath('//*[@id="appMountPoint"]/div/div/div[1]/div[1]/div[1]/div/div/div/div[1]/div/div/input').send_keys(title)
time.sleep(1)
#Enter in the search field
driver.find_element_by_css_selector("#title-card-0-0 > div.ptrack-content").click()
time.sleep(2)
driver.find_element_by_css_selector("#pane-Overview > div > div > div > div.ptrack-content > div > div.jawbone-actions > a.playLink.isToolkit > button").click()
#Click play
This completes the Netflix autoplay code !!
Since I am a beginner, I think I can still simplify the code, but since it was completed at the beginning, I would appreciate your understanding. Next time, I would like to create a GUI with python, get a text box, and click a button to perform automatic playback.
Thank you for your relationship.
Recommended Posts