selenium.common.exceptions.TimeoutException:消息:尝试打印节目标题时出错
selenium.common.exceptions.TimeoutException: Message: Error trying to print out titles of shows
我在 运行 这个简短的脚本中看到上面的错误:
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 as EC
url ='https://animelon.com/'
PATH = 'C:\Program Files (x86)\chromedriver.exe'
driver = webdriver.Chrome(PATH)
driver.get(url)
try:
section = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, 'row ng-show-toggle-slidedown series-content-container'))
)
anime = section.find_elements_by_class_name('col-lg-3 col-md-4 col-sm-6 col-xs-12 mini-previews ng-scope')
for show in anime:
header = show.find_element_by_class_name('anime-name ng-binding')
print(header.text)
finally:
driver.quit()
我看到了这个错误的不同答案,但它们都太过case-specific所以我决定自己做一个post。如果您有任何方法可以解决此错误,请告诉我。提前致谢!
编辑:我尝试简单地将超时从 10 增加到 30 等等。但是出现同样的错误
您使用了错误的定位器。
由于您通过多个 class 名称定位元素,因此您应该使用 CSS_SELECTOR,而不是 By.CLASS_NAME
此外,您应该等待元素可见性,而不仅仅是存在。
此外,要搜索元素内部的元素,建议使用以点 .
开头的 XPath,就像我在此处使用的那样。
看看这是否适合您:
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 as EC
url ='https://animelon.com/'
PATH = 'C:\Program Files (x86)\chromedriver.exe'
driver = webdriver.Chrome(PATH)
driver.get(url)
try:
section = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, '.row.ng-show-toggle-slidedown.series-content-container'))
)
anime = section.find_elements_by_xpath('.//*[@class="col-lg-3 col-md-4 col-sm-6 col-xs-12 mini-previews ng-scope"]')
for show in anime:
header = show.find_element_by_xpath('.//*[@class="anime-name ng-binding"]')
print(header.text)
finally:
driver.quit()
我在 运行 这个简短的脚本中看到上面的错误:
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 as EC
url ='https://animelon.com/'
PATH = 'C:\Program Files (x86)\chromedriver.exe'
driver = webdriver.Chrome(PATH)
driver.get(url)
try:
section = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, 'row ng-show-toggle-slidedown series-content-container'))
)
anime = section.find_elements_by_class_name('col-lg-3 col-md-4 col-sm-6 col-xs-12 mini-previews ng-scope')
for show in anime:
header = show.find_element_by_class_name('anime-name ng-binding')
print(header.text)
finally:
driver.quit()
我看到了这个错误的不同答案,但它们都太过case-specific所以我决定自己做一个post。如果您有任何方法可以解决此错误,请告诉我。提前致谢!
编辑:我尝试简单地将超时从 10 增加到 30 等等。但是出现同样的错误
您使用了错误的定位器。
由于您通过多个 class 名称定位元素,因此您应该使用 CSS_SELECTOR,而不是 By.CLASS_NAME
此外,您应该等待元素可见性,而不仅仅是存在。
此外,要搜索元素内部的元素,建议使用以点 .
开头的 XPath,就像我在此处使用的那样。
看看这是否适合您:
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 as EC
url ='https://animelon.com/'
PATH = 'C:\Program Files (x86)\chromedriver.exe'
driver = webdriver.Chrome(PATH)
driver.get(url)
try:
section = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, '.row.ng-show-toggle-slidedown.series-content-container'))
)
anime = section.find_elements_by_xpath('.//*[@class="col-lg-3 col-md-4 col-sm-6 col-xs-12 mini-previews ng-scope"]')
for show in anime:
header = show.find_element_by_xpath('.//*[@class="anime-name ng-binding"]')
print(header.text)
finally:
driver.quit()