Home > OS >  Click share button under youtube video with selenium
Click share button under youtube video with selenium

Time:05-27

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
import pyautogui

titleVideo = input("Enter the title of the video: ")
chrome_options = webdriver.ChromeOptions()

prefs = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_experimental_option("prefs", prefs)
# Add experimental options to remove "Google Chrome is controlled by automated software" notification
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
driver = webdriver.Chrome(r'C:\Users\iwanh\Desktop\Drivers\chromedriver.exe', options=chrome_options)

driver.get("https://www.youtube.com/")

# We use driver.find_element with the help of the By import instead of find_element_by_name or id

accept_all = driver.find_element(By.XPATH, '/html/body/ytd-app/ytd-consent-bump-v2-lightbox/tp-yt-paper-dialog/div['
                                           '4]/div/div[6]/div[1]/ytd-button-renderer['
                                           '2]/a/tp-yt-paper-button/yt-formatted-string')
accept_all.click()

time.sleep(5)

search_box = driver.find_element(By.XPATH, value='/html/body/ytd-app/div[1]/div/ytd-masthead/div[3]/div['
                                                 '2]/ytd-searchbox/form/div[1]/div[1]/input')

search_box.send_keys(titleVideo)

searchGo = driver.find_element(By.XPATH, value='/html/body/ytd-app/div[1]/div/ytd-masthead/div[3]/div['
                                               '2]/ytd-searchbox/button/yt-icon')
searchGo.click()
time.sleep(3)
pyautogui.press('tab')
pyautogui.press('tab')  # Slopppy way to click on the first recommended
pyautogui.press('enter')# video will fix later
time.sleep(3)
shareButton = driver.find_element(By.XPATH, "/html/body/ytd-app/div[1]/ytd-page-manager/ytd-watch-flexy/div[5]/div["
                                            "1]/div/ytd-watch-metadata/div/div[2]/div["
                                            "2]/div/div/ytd-menu-renderer/div[1]/ytd-button-renderer["
                                            "1]/a/yt-icon-button/yt-interaction")

time.sleep(3)
shareButton.click()

time.sleep(2.5)
copyButton = driver.find_element(By.XPATH, value="/html/body/ytd-app/ytd-popup-container/tp-yt-paper-dialog["
                                                 "1]/ytd-unified-share-panel-renderer/div["
                                                 "2]/yt-third-party-network-section-renderer/div["
                                                 "2]/yt-copy-link-renderer/div/yt-button-renderer/a/tp-yt-paper"
                                                 "-button/yt-formatted-string")
copyButton.click()

When it executes the shareButton part it says that it is "Unable to locate element". Two things that I am suspiscious might be happening.

  1. I am not copying the right XPATH
  2. The XPATH changes everytime I open a new chrome tab OR everytime i rerun the program.

Output:

Share button I want its XPATH:

P.S. I have tried to find element with other ways than XPATH but same result, if someones manages to do it with another way, its perfect.

CodePudding user response:

you can click the button to share with:

driver.find_element(By.XPATH, value='//*[@id="top-level-buttons-computed"]/ytd-button-renderer[1]/a').click()

though a major issue is that the element is not loaded on the page when going to the page first. So you have to wait for that share element to load you could do that a couple of ways using: (easier method)

import time
time.sleep(10) # to wait 10 seconds

or (the recommended way)

Wait until page is loaded with Selenium WebDriver for Python

  • Related