使用selenium和python打开youtube视频链接实现Web自动化

2024-05-14 03:46:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我想打开一个youtube视频。导航到搜索结果后,我无法打开所需的链接。它不接受searchBaari的xpath。任何帮助都将不胜感激

代码如下:

from selenium import webdriver

driver = webdriver.Chrome('/Users/souravroy/Downloads/chromedriver-2')

driver.get('https://youtube.com')

searchbox = driver.find_element_by_xpath('//*[@id="search"]')

searchbox.send_keys('Baari')

searchButton = driver.find_element_by_xpath('//*[@id="search-icon-legacy"]/yt-icon')

searchButton.click()

driver.implicitly_wait(4500)

searchBaari = driver.find_element_by_xpath('//*[@id="Baari Lyrics Translation"]/yt-formatted-string')

searchBaari.Click()

Tags: idsearchbyyoutubedriverelementfindxpath
1条回答
网友
1楼 · 发布于 2024-05-14 03:46:48

我在页面上没有找到任何带有@id="Baari Lyrics Translation"的元素。虽然有一个链接与这样的标题部分匹配。尝试修改最后几行以使用此定位器:

searchBaari = driver.find_element_by_xpath('//a[contains(@title,"Baari Lyrics Translation")]')
searchBaari.click()

这应该会成功的,我希望它能有所帮助。祝你好运

相关问题 更多 >