Selenium返回到for循环中的上一页



我正在制作一个抓取器来抓取博彩网站的数据,这是一段将抓取事件、团队和赔率的示例代码。这个想法是迭代所有可用于这项运动的比赛。我可以找到链接并点击第一个,然后我也可以抓取数据并将其返回到列表中(稍后放入数据库(。我的问题是我无法返回上一页单击下一个链接,我在尝试执行此操作时出错。这是我的代码:

driver = webdriver.Chrome(Path)
driver.get("https://www.neds.com.au/sports/table-tennis/")
a = []
links = driver.find_elements_by_class_name("matches-filter__link")
for l in links:

l.click()
tt_matches = driver.find_elements_by_class_name("sport-event-card")
for match in tt_matches:
Match = match.find_element_by_css_selector(".sports-event-title__name-text").text
a.append(Match)
Teams = match.find_elements_by_css_selector(".price-button")
for team in Teams:
team_name = team.find_element_by_css_selector(".price-button-name").text
team_odd = team.find_element_by_css_selector(".price-button-odds-price span").text
a.append(team_name)
a.append(team_odd)
driver.back()
time.sleep(2)
driver.quit()

并返回此错误:

引发exception_class(消息、屏幕、堆栈(

陈旧元素引用异常:陈旧元素引用:元素为未附加到页面文档(会话信息:铬=84.0.4147.105(

我该如何解决这个问题,我认为问题出在driver.back((上,我也尝试过l.back((,但它仍然不起作用。

在这种情况下,您需要收集URL,然后导航到它们。您可以使用以下代码:

driver.get("https://www.neds.com.au/sports/table-tennis/")
time.sleep(5)
a = []
links = driver.find_elements_by_class_name("matches-filter__link")
urls = [l.get_attribute('href') for l in links]
for u in urls:
print(u)
driver.get(u)
tt_matches = driver.find_elements_by_class_name("sport-event-card")
for match in tt_matches:
Match = match.find_element_by_css_selector(".sports-event-title__name-text").text
a.append(Match)
Teams = match.find_elements_by_css_selector(".price-button")
for team in Teams:
team_name = team.find_element_by_css_selector(".price-button-name").text
team_odd = team.find_element_by_css_selector(".price-button-odds-price span").text
a.append(team_name)
a.append(team_odd)
time.sleep(2)
print(a)
driver.quit()

相关内容

  • 没有找到相关文章

最新更新