使用 Python 在 Selenium 中查找元素时遇到问题 [英] Trouble finding element in Selenium with Python
问题描述
我一直在尝试收集 Youtube Gaming 上的直播频道/观众列表.我在 Python 中使用 selenium 来强制网站向下滚动页面,以便加载超过 11 个频道.作为参考,这个是我正在开发的网页.
我已经找到了我想要的数据的位置,但是我正在努力让 selenium 去那里.我遇到问题的部分如下所示:
<div class="style-scope ytg-gaming-video-renderer" id="video-metadata"><span class="title ellipsis-2 style-scope ytg-gaming-video-renderer"><ytg-nav-endpoint class="style-scope ytg-gaming-video-renderer x-scope ytg-nav-endpoint-2"><a href="/watch?v=FFKSD1HHrdA" tabindex="0" class="style-scope ytg-nav-endpoint" target="_blank">现场遇见Bo3</a></ytg-nav-endpoint></span><div class="channel-info 小布局水平居中样式范围 ytg-gaming-video-renderer"><ytg-owner-badges class="style-scope ytg-gaming-video-renderer x-scope ytg-owner-badges-0"><template class="style-scope ytg-owner-badges" is="dom-repeat"></template></ytg-owner-badges><ytg-formatted-string class="style-scope ytg-gaming-video-renderer"><ytg-nav-endpoint class="style-scope ytg-formatted-string x-scope ytg-nav-endpoint-2"><a href="/channel/UCD8Q9V5wgo8o0XGfUqsRrDQ" tabindex="0" class="style-scope ytg-nav-endpoint" target="_blank">Rico Eeman</a></ytg-nav-endpoint></ytg-formatted-string></div><span class="ellipsis-1 small style-scope ytg-gaming-video-renderer" id="video-viewership-info" hidden=""></span><div id="metadata-badges" class="small style-scope ytg-gaming-video-renderer"><ytg-live-badge-renderer class="style-scope ytg-gaming-video-renderer x-scope ytg-live-badge-renderer-1"><template class="style-scope ytg-live-badge-renderer" is="dom-if"></template><span aria-label="" class="文本布局水平居中样式范围 ytg-live-badge-renderer">4 观看</span><template class="style-scope ytg-live-badge-renderer" is="dom-if"></template></ytg-live-badge-renderer>
目前,我正在尝试:
#这部分工作正常.我可以使用唯一 IDmeta_data = driver.find_element_by_id('视频元数据')#这部分也不错.再一次,它有一个ID.观众 = meta_data.find_element_by_id('元数据-徽章')打印(观众.文本)
但是,我一直无法获取频道名称(在本例中为 'Rico Eeman'
,它位于第一个嵌套的 div 标签下).因为它是一个复合类名,我无法通过类名找到该元素,并且尝试以下 xpaths 不起作用:
name = meta_data.find_element_by_xpath('/div[@class="channel-info 小布局水平居中样式范围 ytg-gaming-video-renderer"]/ytg-formatted-string'name = meta_data.find_element_by_xpath('/div[1])
它们都引发了未找到元素的错误.我不确定在这里做什么.有人有可行的解决方案吗?
<小时>名称 id 不在 <ytg-formatted-string>
标签中,它在它的后代之一中.试试
meta_data.find_element_by_css_selector('.style-scope.ytg-formatted-string.x-scope.ytg-nav-endpoint-2 > a')
或者使用 xpath
meta_data.find_element_by_xpath('//ytg-nav-endpoint[@class="style-scope ytg-formatted-string x-scope ytg-nav-endpoint-2"]/a')
I have been trying to collect a list of live channels/viewers on Youtube Gaming. I am using selenium with Python to force the website to scroll down the page so it loads more that 11 channels. For reference, this is the webpage I am working on.
I have found the location of the data I want, but I am struggling with getting selenium to go there. The part I am having trouble with looks like this:
<div class="style-scope ytg-gaming-video-renderer" id="video-metadata"><span class="title ellipsis-2 style-scope ytg-gaming-video-renderer"><ytg-nav-endpoint class="style-scope ytg-gaming-video-renderer x-scope ytg-nav-endpoint-2"><a href="/watch?v=FFKSD1HHrdA" tabindex="0" class="style-scope ytg-nav-endpoint" target="_blank">
Live met Bo3
</a></ytg-nav-endpoint></span>
<div class="channel-info small layout horizontal center style-scope ytg-gaming-video-renderer">
<ytg-owner-badges class="style-scope ytg-gaming-video-renderer x-scope ytg-owner-badges-0">
<template class="style-scope ytg-owner-badges" is="dom-repeat"></template>
</ytg-owner-badges>
<ytg-formatted-string class="style-scope ytg-gaming-video-renderer">
<ytg-nav-endpoint class="style-scope ytg-formatted-string x-scope ytg-nav-endpoint-2"><a href="/channel/UCD8Q9V5wgo8o0XGfUqsRrDQ" tabindex="0" class="style-scope ytg-nav-endpoint" target="_blank">Rico Eeman</a>
</ytg-nav-endpoint>
</ytg-formatted-string>
</div><span class="ellipsis-1 small style-scope ytg-gaming-video-renderer" id="video-viewership-info" hidden=""></span>
<div id="metadata-badges" class="small style-scope ytg-gaming-video-renderer">
<ytg-live-badge-renderer class="style-scope ytg-gaming-video-renderer x-scope ytg-live-badge-renderer-1">
<template class="style-scope ytg-live-badge-renderer" is="dom-if"></template>
<span aria-label="" class="text layout horizontal center style-scope ytg-live-badge-renderer">4 watching</span>
<template class="style-scope ytg-live-badge-renderer" is="dom-if"></template>
</ytg-live-badge-renderer>
</div>
</div>
Currently, I am trying:
#This part works fine. I can use the unique ID
meta_data = driver.find_element_by_id('video-metadata')
#This part is also fine. Once again, it has an ID.
viewers = meta_data.find_element_by_id('metadata-badges')
print(viewers.text)
However, I am have been having trouble getting to the channel name (in this example 'Rico Eeman'
, and it is under the first nested div tag). Because its a compound class name, I cannot find the element by class name, and trying the following xpaths doesnt work:
name = meta_data.find_element_by_xpath('/div[@class="channel-info small layout horizontal center style-scope ytg-gaming-video-renderer"]/ytg-formatted-string'
name = meta_data.find_element_by_xpath('/div[1])
They both raise the element not found error. I am not really sure what to do here. Does anyone have a working solution?
The name id not in the <ytg-formatted-string>
tag, its in one of it descendants. Try
meta_data.find_element_by_css_selector('.style-scope.ytg-formatted-string.x-scope.ytg-nav-endpoint-2 > a')
Or with xpath
meta_data.find_element_by_xpath('//ytg-nav-endpoint[@class="style-scope ytg-formatted-string x-scope ytg-nav-endpoint-2"]/a')
这篇关于使用 Python 在 Selenium 中查找元素时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!