Selenium 只返回一个空列表 [英] Selenium only returns an empty list

查看:27
本文介绍了Selenium 只返回一个空列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 betfair.com 抓取足球队的名称,但无论如何,它都会返回一个空列表.这是我最近尝试过的.

I'm trying to scrape football team names from betfair.com and no matter what, it returs an empty list. This is what I've tried most recently.

from selenium import webdriver
import pandas as pd

driver = webdriver.Chrome(r'C:\Users\Tom\Desktop\chromedriver\chromedriver.exe')
driver.get('https://www.betfair.com/exchange/plus/football')

team = driver.find_elements_by_xpath('//*[@id="main-wrapper"]/div/div[2]/div/ui-view/div/div/div/div/div[1]/div/div[1]/bf-super-coupon/main/ng-include[3]/section[1]/div[2]/bf-coupon-table/div/table/tbody/tr[1]/td[1]/a/event-line/section/ul[1]/li[1]')

print(team)

推荐答案

你应该使用 WebDriverWait.此外,您应该使用相对 xPath,而不是绝对 xPath.您正在为单个元素使用 find_elements 的另一件事.

You should use WebDriverWait. Also, you should use a relative xPath, not absolute xPath. One more thing you are using find_elements for a single element.

这里我正在打印所有团队

Here I'm printing all the teams

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

driver = webdriver.Chrome(r"C:\Users\Tom\Desktop\chromedriver\chromedriver.exe")
driver.get('https://www.betfair.com/exchange/plus/football')
teams = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, '//*[@id="main-wrapper"]//ul[@class="runners"]/li')))
print([i.text for i in teams])

输出:

['Everton',
 'West Ham',
 'Tottenham',
 'Watford',
 'Chelsea',
 'Newcastle',
 'Wolves',
 'Southampton',
 'Leicester',
 'Burnley',
 'Aston Villa',
 'Brighton',
 'Bournemouth',
 'Norwich',
 'Crystal Palace',
 'Man City',
 'Man Utd',
 'Liverpool',
 'Sheff Utd',
 'Arsenal',
 'Eintracht Frankfurt',
 'Leverkusen',
 'Werder Bremen',
 'Hertha Berlin',
 'Augsburg',
 'Bayern Munich',
 'Fortuna Dusseldorf',
 'Mainz',
 'RB Leipzig',
 'Wolfsburg',
 'Union Berlin',
 'Freiburg',
 'Dortmund',
 'Mgladbach',
 'FC Koln',
 'Paderborn',
 'Hoffenheim',
 'Schalke 04',
 'St Etienne',
 'Lyon',
 'Nice',
 'Paris St-G',
 'Lyon',
 'Dijon',
 'Reims',
 'Montpellier',
 'Nimes',
 'Amiens',
 'Toulouse',
 'Lille',
 'Metz',
 'Nantes',
 'Angers',
 'Brest',
 'Bordeaux',
 'St Etienne',
 'Monaco',
 'Rennes',
 'Houston Dynamo',
 'LA Galaxy',
 'Philadelphia',
 'New York City',
 'Atlanta Utd',
 'New England',
 'Seattle Sounders',
 'Minnesota Utd',
 'DC Utd',
 'FC Cincinnati',
 'Orlando City',
 'Chicago Fire',
 'Montreal Impact',
 'New York Red Bulls',
 'Toronto FC',
 'Columbus',
 'Los Angeles FC',
 'Colorado',
 'FC Dallas',
 'Kansas City',
 'Shakhtar',
 'Dinamo Zagreb',
 'Atletico Madrid',
 'Leverkusen',
 'Club Brugge',
 'Paris St-G',
 'Tottenham',
 'Crvena Zvezda',
 'Olympiakos',
 'Bayern Munich',
 'Man City',
 'Atalanta',
 'Galatasaray',
 'Real Madrid',
 'Juventus',
 'Lokomotiv',
 'Ajax',
 'Chelsea',
 'RB Leipzig',
 'Zenit St Petersburg']

这篇关于Selenium 只返回一个空列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆