使用 selenium 和 python 实现 YouTube 自动化,从搜索到的视频中进行选择时出现问题

YouTube automation with selenium and python, problem in selecting from the searched video

目前,在 Youtube 上搜索后,我正在使用 Python 和 Selenium 实现 Youtube 的自动化。 我想根据用户需求从搜索到的视频中 select,即第一个视频、第二个或第三个视频等。 但是我几乎尝试了所有 select or,它们具有相同的属性,它们播放第一个视频。因此,如果您有任何解决方案,请回复。 提前致谢! 下面附上图:) 代码:

select_video1=driver.find_element_by_xpath('//*[@id="video-title"]/yt-formatted-string')
select_video1.click()

图表:

以下应该有效:

driver.get("https://www.youtube.com/results?search_query=decorater")

video_number = 2

driver.find_element_by_xpath(f"(//a[@id='video-title'])[{video_number}]").click()

这使用 f-strings,因此您可以使值动态化。只需将 video_number 更改为您要在列表中播放的视频