如何通过python selenium解析随机类名元素?

问题描述 投票:0回答:1

虽然有一些关于这个主题的文章,但我找不到如何解决我的问题,有人帮助我吗? HTML 源如下所示,我可以使用 获取 'div style="height:100%;overflow:hidden"

' 的元素
xpath = '//div[@style="height:100%;overflow:hidden"]'
divList1 = driver.find_elements(By.XPATH, xpath)

但是我无法使用相同的代码获取 'div style="display: flex; margin-right: 50px; width: 218px;"' 的元素

xpath = '//div[@style="display: flex; margin-right: 50px; width: 218px;"]'
divList1 = driver.find_elements(By.XPATH, xpath)

我也尝试了如下代码,但divList3没有任何元素

xpath = '//div[@style="height:100%;overflow:hidden"]'
divList1 = driver.find_elements(By.XPATH, xpath)
divList2 = divList1[0].find_elements(By.XPATH, './/div')
divList3 = divList2[0].find_elements(By.XPATH, './/div')

所有带有“_*”的类名都是随机的,所以我不能使用这些类名来查找元素,我怎样才能得到“06-28”,“08-09”和“08-21” '

<div style="height:100%;overflow:hidden">
    <div style="display: flex; margin-right: 50px; width: 218px;">
        <div class="_1wqbaeb6m">
            <div class="_1wqbaeb1y">
                <div class="_1wqbaeb6p">
                    <div class="_1wqbaeb6q">
                        <span class="_1wqbaeby _1wqbaeb6">Friday</span>
                    </div>
                    <div class="_1wqbaeb6r">
                        <span class="_1wqbaeb10 _1wqbaeb6 _1wqbaeb6s">06-28</span>
                        <span class="_1wqbaeb10 _1wqbaeb6v">¥5780</span>
                    </div>
                </div>
            </div>
        </div>
        <div class="_1wqbaeb6m">
            <div class="_1wqbaeb1y">
                <div class="_1wqbaeb6p">
                    <div class="_1wqbaeb6q">
                        <span class="_1wqbaeby _1wqbaeb6">Friday</span>
                    </div>
                    <div class="_1wqbaeb6r">
                        <span class="_1wqbaeb10 _1wqbaeb6 _1wqbaeb6s">08-09</span>
                        <span class="_1wqbaeb10 _1wqbaeb6v">¥5780</span>
                    </div>
                </div>
            </div>
        </div>
        <div class="_1wqbaeb6m">
            <div class="_1wqbaeb1y">
                <div class="_1wqbaeb6p">
                    <div class="_1wqbaeb6q">
                        <span class="_1wqbaeby _1wqbaeb6">Wednesday</span>
                    </div>
                    <div class="_1wqbaeb6r">
                        <span class="_1wqbaeb10 _1wqbaeb6 _1wqbaeb6s">08-21</span>
                        <span class="_1wqbaeb10 _1wqbaeb6v">¥5780</span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
python selenium-webdriver xpath
1个回答
0
投票

我建议你阅读这个问题的答案。

特别是您可以使用

starts-with(@class, '_')

© www.soinside.com 2019 - 2024. All rights reserved.