“ driver.find_element_by_css_selector”不起作用。如何解决此异常?

时间:2019-05-15 09:05:32

标签: python selenium

我正在提取YouTube上热门视频的数据。我能够提取“第一视频”的数据,但是在提取“第二视频”的数据时会引发异常。如何解决?

from selenium import webdriver
chromedriver="C:/Users/asus/Downloads/chromedriver_win32 (2)/chromedriver"
driver=webdriver.Chrome(chromedriver)
driver.get("https://www.youtube.com/feed/trending")
print("Trending 1: ")
title1=driver.find_element_by_css_selector('#video-title')
print(title1.text)
views1=driver.find_element_by_css_selector('#metadata-line > span:nth-child(1)')
print("Views: ",(views1.text))
up_t1=driver.find_element_by_css_selector('#metadata-line > span:nth-child(2)')
print("Time: ",(up_t1.text))

second_link=driver.find_element_by_css_selector('#grid-container > ytd-video-renderer:nth-child(2)')
second_link.click()
new_second_link=driver.current_url
print(new_second_link)
driver.get(new_second_link)
#error part
#title2=driver.find_element_by_xpath('#count > yt-view-count-renderer > span.view-count.style-scope.yt-view-count-renderer').text
title2=driver.find_element_by_css_selector('#count > yt-view-count-renderer > span.view-count.style-scope.yt-view-count-renderer').text
print(title2)

1 个答案:

答案 0 :(得分:1)

您使用的css选择器是正确的。由于加载页面和webdriver无法找到元素,它引发了错误。

为克服此类问题,始终建议使用WebDriverWaitelement_to_be_clickable

尝试以下代码,它将返回预期结果。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions

chromedriver="C:/Users/asus/Downloads/chromedriver_win32 (2)/chromedriver"
driver=webdriver.Chrome(chromedriver)
driver.get("https://www.youtube.com/feed/trending")
print("Trending 1: ")
title1=driver.find_element_by_css_selector('#video-title')
print(title1.text)
views1=driver.find_element_by_css_selector('#metadata-line > span:nth-child(1)')
print("Views: ",(views1.text))
up_t1=driver.find_element_by_css_selector('#metadata-line > span:nth-child(2)')
print("Time: ",(up_t1.text))

second_link=driver.find_element_by_css_selector('#grid-container > ytd-video-renderer:nth-child(2)')
second_link.click()
new_second_link=driver.current_url
print(new_second_link)
driver.get(new_second_link)

wait=WebDriverWait(driver,30)
title2=wait.until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR,'h1.ytd-video-primary-info-renderer yt-formatted-string'))) #driver.find_element_by_css_selector('h1.ytd-video-primary-info-renderer yt-formatted-string').text
print("Title : " + title2.text)
count=wait.until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR,'#count > yt-view-count-renderer > span.view-count.style-scope.yt-view-count-renderer')))
print("Views Count :" + count.text)

控制台输出

Trending 1: 
Film Theory: The Rat That Beat Thanos! (Marvel Endgame)
Views:  1.2M views
Time:  15 hours ago
https://www.youtube.com/watch?v=UkhYYwF7XaU
Title : Eurovision Song Contest 2019 - First Semi-Final - Live Stream
Views Count :1,290,319 views