使用 python selenium 从 href 检索值

问题描述

<a class="yt-simple-endpoint style-scope yt-formatted-string" spellcheck="false" href="/channel/UC8butISFwT-Wl7EV0hUK0BQ" dir="auto">freeCodeCamp.org</a>

我是 selenium 的新手,正在尝试制作一个 youtube 排名检查机器人! 我试图从这里获取 href 值,以便我可以将它与频道名称进行比较并打印出正确的排名编号,但我得到的输出不正确。 我得到的输出是 2,5,而我应该得到 6,7。

谁能告诉我哪里/我做错了什么?可以做些什么来解决这个问题?提前致谢

下面附截图以查看排名

enter image description here

from selenium import webdriver
import time
channel_name = 'freeCodeCamp.org' #channel name
driver = webdriver.Chrome(r"C:\\Users\\user\\PycharmProjects\\YoutubeRankCheckBot\\Drivers\\chromedriver.exe")
driver.get("http://youtube.com")
driver.maximize_window()

search_bar = driver.find_element_by_id("search")
search_bar.send_keys("React JS") #Inserting text input in a automation way
search_button = driver.find_element_by_id("search-icon-legacy")
search_button.click()

time.sleep(5)


video_list = driver.find_elements_by_xpath('//a[contains(@href,"/channel/UC8butISFwT-Wl7EV0hUK0BQ")]')
print(video_list)

for index,channel in enumerate(video_list):
    if channel.text  == channel_name:
        print(index)

解决方法

 video_list = driver.find_elements_by_xpath('//a[@class="style-scope ytd-video-renderer"]')
video_url = [video_list.get_attribute('href').replace('https://www.youtube.com','') for video_list in video_list]
print(video_url)

for index,channel in enumerate(video_url):
    if channel == channel_id:
        print(index)

这里的 channel_id 只是频道名称 /channel/UC8butISFwT-Wl7EV0hUK0BQfreeCodeCamp.org

  1. 第一行给出了 a 标签的所有元素
  2. 从第 1 步的列表中,找到 href 元素。它将为您提供完整的 URL,例如https://www.youtube.com/channel/UC8butISFwT-Wl7EV0hUK0BQ。所以替换 https://www.youtube.com 并提取到 video_url
  3. video_url 枚举并打印 index
,

您使用了错误的定位器。
试试这个:

video_list = driver.find_elements_by_xpath("//div[@id='channel-info']//a[@class='yt-simple-endpoint style-scope yt-formatted-string']")

UPD
经过你的解释,我明白了一些。
您可以通过以下方式找到这些特定元素:

//div[@id='channel-info']//a[@class='yt-simple-endpoint style-scope yt-formatted-string' and (contains(@href,"/channel/UC8butISFwT-Wl7EV0hUK0BQ"))]

这将为您提供 2 个元素,因为该搜索结果中有来自该频道的 2 个视频

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...