单击带有硒的 youtube 视频下的共享按钮

Click share button under youtube video with selenium

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()

当它执行 shareButton 部分时,它说它是“无法定位元素”。我怀疑的两件事可能正在发生。

  1. 我没有复制正确的 XPATH
  2. 每次我打开一个新的 chrome 选项卡时,XPATH 都会改变 或者 每次我重新运行程序时。

输出:

分享按钮我想要它的 XPATH:

P.S。我尝试用 XPATH 以外的其他方法查找元素,但结果相同,如果有人设法用另一种方法找到它,那就太完美了。

您可以点击按钮分享给:

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

虽然一个主要问题是当第一次转到页面时元素没有加载到页面上。所以你必须等待那个共享元素加载你可以使用以下几种方法来做到这一点:(更简单的方法)

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

或(推荐方式)

Wait until page is loaded with Selenium WebDriver for Python