使用硒解析嵌套元素不起作用 - python [英] Parsing nested elements using selenium not working - python

查看:28
本文介绍了使用硒解析嵌套元素不起作用 - python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所附图片显示了我试图抓取的 HTML 页面的结构:

The picture attached show the structure of the HTML page that I am trying to scrape:

首先,我检索元素 league-item,然后我正在寻找具有类名称的 i 项目:'ds-icon-material League-toggle-icon'

First I retrieve the element league-item and then I am looking for the i item with class name : 'ds-icon-material league-toggle-icon'

Selenium 告诉我它找不到任何具有此类名称的项目.这是我的代码:

Selenium is telling me that it cannot find any item with such name. Here is my code:

path = r"""chromedriver.exe"""
driver = webdriver.Chrome(executable_path=path)
driver.get(_1bet)
time.sleep(5)
#a = driver.find_element_by_class_name('box-content.box-bordered.box-stick.box-bordered-last')

league1 = driver.find_elements_by_class_name('league-list')[0]
league1.find_element_by_class_name("ds-icon-material league-toggle-icon")

你能帮我吗?我不明白为什么它不起作用.

Can you please help me? I dont understand why it isn't working.

谢谢

注意:我正在抓取的网站是:https://1bet.com/ca/sports/tennis?time_range=all

NB: The website I'm scraping is: https://1bet.com/ca/sports/tennis?time_range=all

推荐答案

我无法访问该网页,所以我只能猜测那里发生了什么.
我可以在这里解决两个问题:

I can't access that web page so I can only guess what is going there.
I can figure 2 problems here:

  1. 要选择元素内的元素,最好使用以点 开头的 XPath.
  2. 您尝试访问的元素有 2 个类名.您应该使用 css 选择器或 XPath 根据多个类名定位元素.
    所以我建议你试试这个:

league1 = driver.find_elements_by_class_name('league-list')[0]
league1.find_element_by_xpath(".//i[@class='ds-icon-material league-toggle-icon']")

这篇关于使用硒解析嵌套元素不起作用 - python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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