+ 4

How to search and open a Youtube video with selenium?

import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC driver = webdriver.Chrome() driver.maximize_window() wait = WebDriverWait(driver, 3) presence = EC.presence_of_element_located visible = EC.visibility_of_element_located # Navigate to url with video being appended to search_query video = 'dragon city best hack tool' driver.get("https://www.youtube.com/results?search_query=" + str(video)) # play the video driver.find_elements_by_xpath('/html/body/ytd-app/div/ytd-page-manager/ytd-search/div[1]/ytd-two-column-search-results-renderer/div/ytd-section-list-renderer/div[2]/ytd-item-section-renderer/div[3]/ytd-shelf-renderer/div[1]/div[2]/ytd-vertical-list-renderer/div[1]/ytd-video-renderer[2]/div[1]/div/div[1]/div/h3/a').click() time.sleep(20)

29th Jan 2020, 9:38 PM
Rei
Rei - avatar
5 odpowiedzi
+ 4
It's returning more than 1 element and that's why you cannot perform a click. Try to use driver.find_element... or elements[0] to get a single object instead of list of objects
29th Jan 2020, 9:50 PM
r8w9
r8w9 - avatar
+ 2
thank you that was really helpful
29th Jan 2020, 9:55 PM
Rei
Rei - avatar
+ 1
What's the error message?
29th Jan 2020, 9:44 PM
r8w9
r8w9 - avatar
+ 1
Traceback (most recent call last): File "C:\Users\User\Desktop\Projects\Python\Yotube Views\main.py", line 21, in <module> driver.find_elements_by_xpath('/html/body/ytd-app/div/ytd-page-manager/ytd-search/div[1]/ytd-two-column-search-results-renderer/div/ytd-section-list-renderer/div[2]/ytd-item-section-renderer/div[3]/ytd-shelf-renderer/div[1]/div[2]/ytd-vertical-list-renderer/div[1]/ytd-video-renderer[2]/div[1]/div/div[1]/div/h3/a').click() AttributeError: 'list' object has no attribute 'click' i want to know if is any way to click to video after you search on youtube
29th Jan 2020, 9:45 PM
Rei
Rei - avatar