Selenium Python 在不同条件下将 Text-var 链接到子子中的 Value-var



我有以下代码:

<div class="_97KWR">
<div class="_12x3s">
<div class="_1PF0y">
<div class="_1xNd0 CTwrR _3ASIK">
<a class="_3bf_k _1WIu4" href="/en/gilgamesh/hola-dola/gof-turkey-championship-2019-555/live/gazorpazorp/log-arena-fams-5551565"><span>Franco Pescatore</span></a>
<div class="_2cZpo">
<svg width="32px" height="32px" xmlns="http://www.w3.org/2000/svg">
<use xlink:href="dist/icons.svg#hula-hula"></use>
</svg>
</div>
<div class="_1sT8o _2ALVH">
<div class="_2Li3l _1fAz9"><img src="/dist/disabled-gg.png" alt="Disabled gg"></div>
</div>
</div>
<div class="_2b9x1">
<div>
<svg width="18px" height="14px" xmlns="http://www.w3.org/2000/svg">
<use xlink:href="dist/icons.svg#vs"></use>
</svg>
</div>
</div>
<div class="_1xNd0 CTwrR _3ASIK">
<a class="_3bf_k _1WIu4" href="/en/gilgamesh/hola-dola/gof-turkey-championship-2019-555/live/gazorpazorp/log-arena-fams-5551565"><span>Giorgio Pescato</span></a><img class="_25rWl _2cZpo" alt="Giorgio Pescato" src="https://ultramedia.com/Media/GiorgioPescato_f6f84978-6da4-4e14-a36b-860dce530d08.png">
<div class="_1sT8o _2ALVH">
<div class="_2Li3l _1fAz9"><img src="/dist/disabled-gg.png" alt="Disabled gg"></div>
</div>
</div>
</div>
<div class="_1xNd0 CTwrR">
<a class="_3bf_k _1WIu4" href="'/en/gilgamesh/hola-dola/swing/fun#4844844"> 
<span>BipolarFun</span></a><img class="_25rWl _2cZpo" alt="BipolarFun" src="https://promedia.com/Media//en/gilgamesh/hola-dola/swing/4844844.png">
<div class="_1sT8o _2ALVH">
<button class="_2Li3l _1fAz9"><span>1,30</span></button>
<div class="_1CeGR">
<div class="_1T5lR"></div>
</div>
</div>
</div>

它以这种方式动态地进行, 由于 DIV 的类是动态的,因此我使用xpath"修复"

/

/a[starts-with(@href,'/en/gilgamesh/hola-dola/

有了这个,我可以像佛朗哥·佩斯卡托雷或乔治·佩斯卡托一样访问名字 和xpath

/

/button[starts-with(@class,'_'(]

我可以访问该值,但当它处于"禁用"状态时无法访问

我也不能先做,然后再做其他的,因为它不返回相同数量的元素,所以我在将 Text 元素与子子元素中的 Value 元素链接时遇到问题,

最后,Resoult应该是这样的:

n[0] = 'BipolarFun'
a[0] = 1,30
n[1] = 'Franco Pescatore'
a[1] = 0 #disabled
n[2] = 'Giorgio Pescato'
a[2] = 0 #disabled

请帮忙,我被困住了:|

您可以尝试的代码:

# get all labels
labels = driver.find_elements_by_xpath("//a[contains(@href,'/en/gilgamesh/hola-dola/')]")
for label in labels:
# get buttons inside label's parent DIV
buttons = label.find_elements_by_xpath("./ancestor::div[1]//button")
# if buttons more than 0, then get button text else use 0.
button_value = buttons[0].text if len(buttons) > 0 else 0
print(f"Label: {label.text}, value: {button_value}")

相关内容

  • 没有找到相关文章

最新更新